Environment.GetCommandLineArgs 方法
返回包含当前进程的命令行参数的字符串数组。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function GetCommandLineArgs As String()
用法
Dim returnValue As String()
returnValue = Environment.GetCommandLineArgs
public static string[] GetCommandLineArgs ()
public:
static array<String^>^ GetCommandLineArgs ()
public static String[] GetCommandLineArgs ()
public static function GetCommandLineArgs () : String[]
返回值
字符串数组,其中的每个元素都包含一个命令行参数。第一个元素是可执行文件名,后面的零个或多个元素包含其余的命令行参数。
异常
异常类型 | 条件 |
---|---|
系统不支持命令行参数。 |
备注
数组中的第一个元素包含执行程序的文件名。如果该文件名不可用,则第一个元素等于 String.Empty。其他元素包含在命令行输入的任何附加标记。
程序文件名可以(但不是必须)包含路径信息。
除非在引号 (QUOTATION MARK, U+0022) 中出现空白,否则用空白(SPACE (U+0020) 或 CHARACTER TABULATION (U+0009))分隔命令行参数。引号中的空白被视为引用文本的一部分。分隔符不包括在命令行参数之中。
引号后跟的反斜杠字符 (REVERSE SOLIDUS (U+005C)) 需要分情况解释。如果 2n 个反斜杠后跟引号,命令行参数则包含 n 个反斜杠。而且,如果其前面的文本为非引用文本,则开始引用其后的文本;如果其前面的文本为引用文本,则结束文本引用。如果 2n+1 个反斜杠后跟引号,命令行参数则包含 n 个反斜杠和一个原意引号。如果 n 个反斜杠后未跟引号,命令行参数则包含 n 个反斜杠。
若要获取作为单个字符串的命令行,请使用 CommandLine 属性。
Windows NT 4.0, Windows 2000 平台说明: 可执行文件的名称不包含该路径。
Windows 98, Windows Millennium Edition 平台说明: 可执行文件的名称包含该路径。长文件名(非 8.3 名称)可以缩短到它们的 8.3 表示形式。
示例
' Sample for the Environment.GetCommandLineArgs method
Imports System
Class Sample
Public Shared Sub Main()
Console.WriteLine()
' Invoke this sample with an arbitrary set of command line arguments.
Dim arguments As [String]() = Environment.GetCommandLineArgs()
Console.WriteLine("GetCommandLineArgs: {0}", [String].Join(", ", arguments))
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'C:\>GetCommandLineArgs ARBITRARY TEXT
'
'GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
'
// Sample for the Environment.GetCommandLineArgs method
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
String[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
}
}
/*
This example produces the following results:
C:\>GetCommandLineArgs ARBITRARY TEXT
GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
// Sample for the Environment::GetCommandLineArgs method
using namespace System;
int main()
{
Console::WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
array<String^>^arguments = Environment::GetCommandLineArgs();
Console::WriteLine( "GetCommandLineArgs: {0}", String::Join( ", ", arguments ) );
}
/*
This example produces the following results:
C:\>GetCommandLineArgs ARBITRARY TEXT
GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
// Sample for the Environment.GetCommandLineArgs method
import System.*;
class Sample
{
public static void main(String[] args)
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
String arguments[] = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}",
String.Join(", ", arguments));
} //main
} //Sample
/*
This example produces the following results:
C:\>GetCommandLineArgs ARBITRARY TEXT
GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
.NET Framework 安全性
- EnvironmentPermission 用于访问“Path”环境变量。关联的枚举:EnvironmentPermissionAccess.Read
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0