2009-07-08 164 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;