2009-11-14 70 views
4

我需要寫一些能夠獲得啓動參數的東西,然後爲這些啓動參數做些事情,並且我認爲開關很好,但它只接受ints,它有是一個字符串帶字符串C的開關語句#

這不是實際的代碼,但我想知道如何讓這樣的工作

namespace Simtho 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      switch (Environment.GetCommandLineArgs()) 
      { 

       case "-i": 
        Console.WriteLine("Command Executed Successfully"); 
        Console.Read; 
        break; 
      } 
     } 

    } 
} 
+8

你可以打開字符串就好了。 – 2009-11-14 20:11:02

回答

8

Environment.GetCommandLineArgs()返回一個字符串數組。數組無法打開。嘗試遍歷數組的成員,像這樣:

namespace Simtho 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      foreach (string arg in Environment.GetCommandLineArgs()) 
      { 
       switch (arg) 
       { 

        case "-i": 
         Console.WriteLine("Command Executed Successfully"); 
         Console.Read(); 
         break; 
       } 
      } 
     } 
    } 
} 
5

什麼這樣的事情?

string[] args = Environment.GetCommandLineArgs(); 

if (args.Contains("-i")) 
{ 
    // Do something 
} 
0

Environment.GetCommandLineArgs()返回字符串數組?

或許我錯了,但在內部開啓轉換成的if-else序列串...