我继承了几个控制台应用程序的维护,这些应用程序自然是用static void Main(string[] args)
输入的。但是,代码忽略args
数组,改为从System.Environment.CommandLine
读取命令行参数。
这里有功能上的区别吗?
内容看起来是一样的。如果有什么不同的话,我会怀疑调用System.Environment.CommandLine
会对性能造成一分钟的影响(但这还不足以让我担心或足够在意来衡量)。
更新:我怀疑System.Environment.CommandLine
应该包含可执行文件路径,但我没有看到它...因为我找错地方了。代码也有string[] arrCmdLine = System.Environment.GetCommandLineArgs();
...检查System.Environment.CommandLine.ToLower()
中是否存在"debug“,而所有其他参数都是从GetCommandLineArgs()
中提取的,当我说”为什么不直接使用args[]
?“时,我会把这两个参数混为一谈。
多年来,我一直在苦苦思索解析命令行参数的最佳方法,而它始终是“以正确的顺序放置它们!”jk
发布于 2012-06-15 03:02:55
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
*/
http://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx
args
参数是一个参数数组。因此,虽然您可以解析System.Environment.CommandLine
中的各个参数,但我不确定您为什么要这样做。我能看到的唯一原因是,如果您需要访问Main()
之外的参数,这可能是一个坏主意。您的Main()
方法应该处理参数,并根据需要在应用程序的其余部分传递参数。
https://stackoverflow.com/questions/11039649
复制相似问题