2009-11-14 5 views
4

시작 인자를 얻고 그 시작 인자에 대해 뭔가를 할 필요가있다. 스위치가 좋을 것이라고 생각했지만 int를 받아 들일 뿐이다. 문자열문자열로 스위치 문을 C#

이 실제 코드가 아닙니다이 될하지만 난이 일

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

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

    } 
} 
+8

문자열을 잘 켤 수 있습니다. –

답변

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()는 문자열 배열을 반환합니까?

은 어쩌면 내가

0

Environment.GetCommandLineArgs() 당신은 문자열 배열에 전환 할 수있는 string[]

반환 ... 틀렸다 내부적 경우 - 다른 순서로 변환 문자열을 전환합니다. 배열에 특정 값이 포함되어 있는지 테스트하고 싶을 수도 있습니다.