2011-05-10 50 views

回答

6

如果你指的是具有一個命令行看起來像這樣: C:> YourProgram.exe /交換機1:數值1 /交換機2:值2 ...

這可以很容易地在啓動時的東西看起來像這樣解析:

private static void Main(string[] args) 
{ 
    Regex cmdRegEx = new Regex(@"/(?<name>.+?):(?<val>.+)"); 

    Dictionary<string, string> cmdArgs = new Dictionary<string, string>(); 
    foreach (string s in args) 
    { 
     Match m = cmdRegEx.Match(s); 
     if (m.Success) 
     { 
     cmdArgs.Add(m.Groups[1].Value, m.Groups[2].Value); 
     } 
    } 
} 

然後,您可以在cmdArgs字典中進行查找。不知道這是你想要的,但。

// daniel

3

從命令行精確傳遞鍵/值對沒有好方法。唯一可用的是一個字符串數組,您可以循環並提取爲鍵/值對。

using System; 

public class Class1 
{ 
    public static void Main(string[] args) 
    { 
     Dictionary<string, string> values = new Dictionary<string, string>(); 

     // hopefully you have even number args count. 
     for(int i=0; i < args.Length; i+=2){ 
     { 
      values.Add(args[i], args[i+1]); 
     } 

    } 
} 

,然後調用

Class1.exe KEY1 VALUE1 VALUE2 KEY2

2

應用程序的入口點是一個main方法,可以採取的參數的string[](這些是命令行參數)。

這不能改變。見MSDN

爲了使這方面的工作更容易,可以使用許多命令行幫助程序庫。

一個這樣的庫是來自MONO傢伙的.NET CLImanyothers

+0

還有[Adaptive Console Framework](http://acf.codeplex.com/)。 – 2011-05-10 20:14:03

相關問題