(string [] args)和System.Environment.CommandLine之间有什么区别?

Mic*_*nis 7 .net parameters command-line-arguments

我继承了几个自动输入的控制台应用程序的维护static void Main(string[] args).但是,代码忽略了args数组,而是从中读取命令行参数System.Environment.CommandLine.

这里有功能差异吗?

内容看起来完全相同.如果有的话,我会怀疑通过调用会产生一分钟的性能损失System.Environment.CommandLine(但还不足以让我感到担心或足够谨慎测量).


更新:我怀疑System.Environment.CommandLine应该包含可执行路径,但我没有看到它...因为我在错误的地方看.代码ALSO有string[] arrCmdLine = System.Environment.GetCommandLineArgs();.... System.Environment.CommandLine.ToLower()检查是否存在"调试",而所有其他参数都是从中提取出来的,GetCommandLineArgs()而我在精神上将两者混为一谈,"我为什么不用它args[]?"

多年来,我一直在为解析命令行args的最佳方式而烦恼,因为它始终是"按照正确的顺序放置它们!" [JK]

jru*_*ell 10

System.Environment.CommandLine 包括可执行文件和参数作为单个字符串.

// Sample for the Environment.CommandLine property.
using System;

class Sample 
{
    public static void Main() 
    {
        Console.WriteLine();
        //  Invoke this sample with an arbitrary set of command line arguments.
        Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
    }
}

/*
This example produces the following results:

C:\>env0 ARBITRARY TEXT

CommandLine: env0 ARBITRARY TEXT
*/
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx

args参数是参数数组.因此,虽然您可以解析单个参数System.Environment.CommandLine,但我不确定您为什么要这样做.我能看到的唯一原因是你是否需要访问外面的参数Main(),无论如何这可能是一个坏主意.您的Main()方法应该处理参数,并根据需要将它们传递给应用程序的其余部分.