2013-11-15 3 views
0

잘못된 매개 변수로 .exe를 호출하면 누구나 콘솔 상자에 메시지를 출력 할 수 있습니까?명령 줄 매개 변수가 잘못된 경우 콘솔 창에 메시지 출력

어제, 매우 친절한 사람들이 나를 UI 여기

없이 내 응용 프로그램을 호출하는 방법을 운동 도운 것은 스레드 그래서

command line to make winforms run without UI

에게, 나는 "에 대응하는 내 응용 프로그램을 말한/silent archive = true transcode = true "이며 UI없이 실행됩니다. 큰!

명령을 잘못 입력하면 메시지를 명령 창에 출력 할 수 있습니까? 에서와 같이

: 나는 도스 창에서이 아무것도하지만 디스플레이를 시도

"매개 변수는 다음과 같이 지정해야합니다/침묵 아카이브 = 진정한 트랜스 = 진정한"..

static void Main(string[] args) 
    { 
     if (args.Length > 0) 
     { 
      if (args[0] == "/silent") 
      { 
       bool archive = false; 
       bool transcode = false; 

       try 
       { 
        if (args[1] == "transcode=true") { transcode = true; }; 
        if (args[2] == "archive=true") { archive = true; }; 
        Citrix_API_Tool.Engine.DownloadFiles(transcode, archive); 
       } 
       catch 
       { 
        Console.Write ("Hello"); 
        Console.ReadLine(); 
        return; 
       } 
      } 
     } 
     else 
+0

Console.Write ... 그렇게 할 것입니다. –

+0

@TonyHopkinson : 콘솔에서 이미 분리 된 경우가 아니라 ... WinForms 응용 프로그램이 기본값 인 IIRC. 하나도없이 시작하면 콘솔없이 실행될 바이너리가 정말로 필요합니다 ... 그러나 명령 줄에서 실행되는 경우 현재 콘솔에 응답하십시오. –

+0

@JonSkeet 네가 머리에 못을 박은 것 같아! 비록 당신이 제안한 것을 어떻게 할 것인지 전혀 생각하지 못했습니다! LOL –

답변

0
internal sealed class Program 
{ 
    [DllImport("kernel32.dll")] 
    private static extern bool AttachConsole(int dwProcessId); 

    private const int ATTACH_PARENT_PROCESS = -1; 
    [STAThread] 
    private static void Main(string[] args) 
    { 
    if(false)//This would be the run-silent check. 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new MainForm()); 
    } 
    try 
    { 
     throw new Exception("Always throw, as this tests exception handling."); 
    } 
    catch(Exception e) 
    { 
     if(AttachConsole(ATTACH_PARENT_PROCESS)) 
     { 
     //Note, we write to Console.Error, not Console.Out 
     //Partly because that's what error is for. 
     //Mainly so if our output were being redirected into a file, 
     //We'd write to console instead of there. 
     //Likewise, if error is redirected to some logger or something 
     //That's where we should write. 
     Console.Error.WriteLine();//Write blank line because of issue described below 
     Console.Error.WriteLine(e.Message); 
     Console.Error.WriteLine();//Write blank line because of issue described below 
     } 
     else 
     { 
     //Failed to attach - not opened from console, or console closed. 
     //do something else. 
     } 
    } 
    }  
} 

문제는 콘솔이 이미 사용자로부터 입력을받는 것으로 되돌아 갔을 것입니다. 그러므로 당신은 정말로 당신이 예외를 가질 수 있다면 가능한 한 빨리하려고 노력하고 싶습니다, 그래서 선 아래로 일어날 수있는 예외보다는 빠른 유효성 검사가 바람직합니다.

관련 문제