Environment.GetCommandLineArgs 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回包含当前进程的命令行自变量的字符串数组。
public:
 static cli::array <System::String ^> ^ GetCommandLineArgs();public static string[] GetCommandLineArgs();static member GetCommandLineArgs : unit -> string[]Public Shared Function GetCommandLineArgs () As String()返回
字符串数组,其中的每个元素都包含一个命令行自变量。 第一个元素是可执行文件名,后面的零个或多个元素包含其余的命令行自变量。
例外
系统不支持命令行参数。
示例
以下示例显示应用程序的命令行参数。
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 output like the following:
    
    C:\>GetCommandLineArgs ARBITRARY TEXT
    
      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
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 output like the following:
    C:\>GetCommandLineArgs ARBITRARY TEXT
      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
*/
open System
//  Invoke this sample with an arbitrary set of command line arguments.
let arguments = Environment.GetCommandLineArgs()
String.concat ", " arguments
|> printfn "\nGetCommandLineArgs: %s"
// This example produces output like the following:
//     C:\>GetCommandLineArgs ARBITRARY TEXT
//
//       GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
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
End Class
'This example produces output like the following:
'    
'    C:\>GetCommandLineArgs ARBITRARY TEXT
'    
'      GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
'
注解
数组中的第一个元素包含执行程序的文件名。 如果文件名不可用,则第一个元素等于 String.Empty。 其余元素包含命令行上输入的任何其他标记。
在 .NET 5 及更高版本中,对于单文件发布,第一个元素是主机可执行文件的名称。
程序文件名可以包含路径信息,但不是必需的。
命令行参数由空格分隔。 可以使用双引号 (“) 在参数中包含空格。 但是,单引号 (') 不提供此功能。
如果双引号后跟两个或偶数个反斜杠,则每个继续反斜杠对将替换为一个反斜杠,并删除双引号。 如果双引号后跟奇数个反斜杠(包括一个反斜杠),则前一对将替换为一个反斜杠,并删除剩余的反斜杠;但是,在这种情况下,不会删除双引号。
下表演示如何分隔命令行参数,并假定 MyApp 为当前正在执行的应用程序。
| 命令行上的输入 | 生成的命令行参数 | 
|---|---|
| MyApp alpha beta | MyApp, alpha, beta | 
| MyApp "alpha with spaces" "beta with spaces" | MyApp, alpha with spaces, beta with spaces | 
| MyApp 'alpha with spaces' beta | MyApp, 'alpha, with, spaces', beta | 
| MyApp \\\alpha \\\\"beta | MyApp, \\\alpha, \\beta | 
| MyApp \\\\\"alpha \"beta | MyApp, \\"alpha, "beta | 
若要获取作为单个字符串的命令行,请使用 CommandLine 属性。