2010-02-06 33 views
4

我有一個模塊需要運行一個小的.Net命令行程序來檢查更新。一切都很好,但我無法抑制顯示命令提示輸出。使用.NET從已啓動的命令行應用程序抑制命令窗口

該應用擁有它自己的Windows窗體,如果它檢測到更新,它會彈出。更新需要作爲一個獨立的應用程序運行,因爲它需要與從其啓動的DLL不同的執行上下文。

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + AUTO_UPDATE_EXENAME; 

updater.StartInfo.FileName = path; 
updater.StartInfo.Arguments = AUTO_UPDATE_PARAMETERS; 
updater.StartInfo.CreateNoWindow = false; 
updater.StartInfo.UseShellExecute = false; 
updater.StartInfo.RedirectStandardOutput = true; 
updater.StartInfo.WorkingDirectory = path; 

updater.Start(); 

我已經嘗試了最重要的CreateNoWindowUseShellExecuteRedirectStandardOutput和他們每個人的結果不同的工作組合,在這惱人的黑盒子彈出。該應用程序確實寫入標準輸出,但我只用它進行調試,用戶不應該真正看到它生成的文本。

假設CreateNoWindow和/或RedirectStandardOutput應該防止盒子彈出,但它無論如何設置這些變量。

+0

另請參閱:http://stackoverflow.com/questions/836427/how-to-run-a-c-console-application-with-the-console-hidden/836436#836436 – 2010-02-06 01:20:30

+0

哇,錯過了那一個。謝謝! – 2010-02-06 01:23:36

回答

5

將命令行應用程序設置爲Winforms應用程序,但執行時不要打開窗體,就像您通常那樣。

+0

這不會阻止他寫入標準輸出嗎? – 2010-02-06 01:09:01

+0

@Cory:不,爲什麼? – 2010-02-06 01:13:30

+0

stdout不再需要,我也通過我們的標準日誌記錄界面登錄 – 2010-02-06 01:21:32

3

您可以隱藏這樣的窗口在起動:

using System.Runtime.InteropServices; 

namespace MyConsoleApp {  
    class Program {   

     [DllImport("user32.dll")]   
     public static extern IntPtr FindWindow(string lpClassName, 
               string lpWindowName); 

     [DllImport("user32.dll")]  
     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

     [STAThread()] 
     static void Main(string[] args)   
     {   
      Console.Title = "MyConsoleApp"; 

      if (args.StartWith("-w"))    
      {     
       // hide the console window      
       setConsoleWindowVisibility(false, Console.Title);     
       // open your form      
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false);      
       Application.Run(new frmMain());   
      }   
      // else don't do anything as the console window opens by default  
     }   

     public static void setConsoleWindowVisibility(bool visible, string title)  
     {    
      //Sometimes System.Windows.Forms.Application.ExecutablePath works 
      // for the caption depending on the system you are running under.   
      IntPtr hWnd = FindWindow(null, title); 

      if (hWnd != IntPtr.Zero)    
      {    
       if (!visible)     
        //Hide the window      
        ShowWindow(hWnd, 0); // 0 = SW_HIDE     
       else     
        //Show window again      
        ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA   
      }   
     } 
    } 
} 

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd

0

下面是詢問MAC活動連接上的示例代碼,這是一個控制檯應用程序,沒必要做這個Windows窗體...

 
    public class TestARP 
    { 
     private StringBuilder sbRedirectedOutput = new StringBuilder(); 
     public string OutputData 
     { 
      get { return this.sbRedirectedOutput.ToString(); } 
     } 

     // Asynchronous! 
     public void Run() 
     { 
      System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo(); 
      ps.FileName = "arp"; 
      ps.ErrorDialog = false; 
      ps.Arguments = "-a"; 
      ps.CreateNoWindow = true; // comment this out 
      ps.UseShellExecute = false; // true 
      ps.RedirectStandardOutput = true; // false 
      ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // comment this out 

      using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) 
      { 
       proc.StartInfo = ps; 
       proc.Exited += new EventHandler(proc_Exited); 
       proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived); 
       proc.Start(); 
       proc.WaitForExit(); 
       proc.BeginOutputReadLine(); // Comment this out 
      } 
     } 

     void proc_Exited(object sender, EventArgs e) 
     { 
      System.Diagnostics.Debug.WriteLine("proc_Exited: Process Ended"); 
     } 

     void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) 
     { 
      if (e.Data != null) this.sbRedirectedOutput.Append(e.Data + Environment.NewLine); 
     } 
    } 

現在,看看Run方法,即在異步模式下,並運行作爲一個單一的控制檯W¯¯ indow - 實際上是一個沒有額外窗口彈出的普通控制檯應用程序,請注意註釋,如果要更改這些行,它將變成一個同步進程,很快,您會注意到此控制檯將創建另一個窗口輸出命令arp。因爲它是在異步模式下,輸出被重定向到的數據充塞到StringBuilder實例作進一步處理的事件處理程序...

希望這有助於 最好的問候, 湯姆。

相關問題