2013-11-15 32 views
0

任何人都可以幫助我輸出消息到控制檯時,我的.exe調用錯誤的參數?如果命令行參數不正確,輸出消息到控制檯窗口

昨日,有很善良的人幫我找出如何調用我的應用程序沒有UI

這裏是線程

command line to make winforms run without UI

所以,我已經告訴我的應用程序,以迴應「/silent archive = true transcode = true「,並且無需UI即可運行。大!

如果命令窗口中的命令不正確,是否可以向命令窗口輸出消息?

在 「參數必須這樣指定:/沉默存檔=真轉碼=真正的」

我已經在DOS窗口中試過,但沒有顯示..

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. 
     } 
    } 
    }  
} 

問題在於控制檯已經返回到從用戶那裏接受輸入。因此,如果你想擁有它,你真的想盡可能快地嘗試你的異常,所以一個快速的驗證檢查,而不是可能發生的異常,是可取的。

相關問題