您的位置:首页 > 其它

Environment.GetCommandLineArgs 方法

2013-08-21 23:54 393 查看


返回包含当前进程的命令行参数的字符串数组。

命名空间: System

程序集: mscorlib(在 mscorlib.dll 中)

语法

C#

C++

F#

VB

public static string[] GetCommandLineArgs()



返回值

类型:System.String[]

字符串数组,其中的每个元素都包含一个命令行参数。 第一个元素是可执行文件名,后面的零个或多个元素包含其余的命令行参数。

异常

异常条件
NotSupportedException系统不支持命令行参数。
备注

数组中的第一个元素包含执行程序的文件名。 如果该文件名不可用,则第一个元素等于 String.Empty。 其他元素包含在命令行输入的任何附加标记。

程序文件名可以(但不是必须)包含路径信息。

命令行参数由空格分隔。 可以使用双引号 (") 在参数中包含空格。 但是,单引号 (') 不提供此功能。

如果两个或偶数个反斜杠后跟双引号,则前面的每个反斜杠对被一个反斜杠替代,并且双引号被删除。 如果奇数个(包括仅仅一个)反斜杠后跟双引号,则前面的每个反斜杠对被一个反斜杠替代,其余的反斜杠被删除;但在此情况下,双引号不会被删除。

下表显示如何分隔命令行参数,并假定 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 属性。

平台说明:可执行文件的名称不包含该路径。

平台说明:可执行文件的名称包含该路径。 长文件名(非 8.3 名称)可以缩短到它们的 8.3 表示形式。

示例

下面的示例显示应用程序的命令行参数。

C#

C++

VB

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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: