2009-07-08 162 views

回答

39

如果proc.StartInfo.UseShellExecute是,那麼你正在啓動過程中,可以使用:

proc.StartInfo.CreateNoWindow = true; 

如果proc.StartInfo.UseShellExecute是,那麼OS是啓動該過程,並且必須通過以下方式向過程提供「提示」:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 

但是, ed應用程序可能會忽略這後一個請求

如果使用UseShellExecute = ,你可能要考慮重定向標準輸出/錯誤,捕獲產生的任何記錄:

proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.RedirectStandardOutput = true; 
proc.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); 
proc.StartInfo.RedirectStandardError = true; 
proc.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); 

,並且有類似

private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) 
{ 
    if (!String.IsNullOrEmpty(outLine.Data)) // use the output outLine.Data somehow; 
} 

的功能有一個良好的頁面覆蓋CreateNoWindow這在an MSDN blog

在Windows中還有一個錯誤,如果您傳遞用戶名/密碼,可能會拋出一個對話框並擊敗CreateNoWindow。有關詳細信息

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476 http://support.microsoft.com/?kbid=818858

+0

有時作品,其他作品不。這是否取決於bat文件命令? – Ahmed 2009-07-08 08:17:48

5

用途: process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

+1

有時候工作,其他人沒有按「T。這是否取決於bat文件命令? – Ahmed 2009-07-08 08:18:56

7

按照Process properties,你有一個:

物業:CreateNoWindow
注意:您可以靜默運行一個命令行程序。 它不會閃爍控制檯窗口。

和:

物業:WindowStyle
注:使用此爲隱藏設置窗口。 作者經常使用ProcessWindowStyle.Hidden

舉一個例子!

static void LaunchCommandLineApp() 
{ 
    // For the example 
    const string ex1 = "C:\\"; 
    const string ex2 = "C:\\Dir"; 

    // Use ProcessStartInfo class 
    ProcessStartInfo startInfo = new ProcessStartInfo(); 
    startInfo.CreateNoWindow = false; 
    startInfo.UseShellExecute = false; 
    startInfo.FileName = "dcm2jpg.exe"; 
    startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2; 

    try 
    { 
     // Start the process with the info we specified. 
     // Call WaitForExit and then the using statement will close. 
     using (Process exeProcess = Process.Start(startInfo)) 
     { 
      exeProcess.WaitForExit(); 
     } 
    } 
    catch 
    { 
     // Log error. 
    } 
} 
+1

有時作品,其他作品不。這是否取決於bat文件命令? – Ahmed 2009-07-08 08:19:31

0

這是對我工作, 當您重定向所有的輸入和輸出,並設置隱藏窗口它應該工作

  Process p = new Process(); 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.RedirectStandardInput = true; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.CreateNoWindow = true;