首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(string[] args)和System.Environment.CommandLine有什么区别?

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

Stack Overflow用户
提问于 2012-06-15 03:00:28
回答 1查看 3K关注 0票数 8

我继承了几个控制台应用程序的维护,这些应用程序自然是用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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-15 03:02:55

System.Environment.CommandLine将可执行文件和参数作为单个字符串包含在内。

代码语言:javascript
复制
// 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()方法应该处理参数,并根据需要在应用程序的其余部分传递参数。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11039649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档