2012-06-14 85 views
5

我正在繼承幾個控制檯應用程序的維護,這些應用程序自然會與static void Main(string[] args)一起輸入。但是,代碼忽略args陣列,而是從System.Environment.CommandLine讀取命令行參數。(string [] args)和System.Environment.CommandLine之間有什麼區別?

這裏有沒有功能差異?

內容看起來完全相同。如果有的話,我會懷疑通過調用System.Environment.CommandLine來進行一分鐘的性能測試(但這還不夠,以至於我永遠不會擔心或不夠小心)。


更新:我懷疑System.Environment.CommandLine應包含可執行文件的路徑,但我沒有看到它...因爲我一直在尋找在錯誤的地方。代碼ALSO有string[] arrCmdLine = System.Environment.GetCommandLineArgs(); .... System.Environment.CommandLine.ToLower()檢查是否存在「調試」,而所有其他參數是從GetCommandLineArgs()中提取的,而我在思考「爲什麼不使用args[]

多年來,我一直在分析命令行參數的最佳方式,當它一直是「把它們放在正確的順序! [jk]

回答

7

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()方法應該處理參數並根據需要將它們傳遞給應用程序的其餘部分。

相關問題