2009-01-12 113 views

回答

14
 Process p = new Process(); 
     StreamReader sr; 
     StreamReader se; 
     StreamWriter sw; 

     ProcessStartInfo psi = new ProcessStartInfo(@"bar.exe"); 
     psi.UseShellExecute = false; 
     psi.RedirectStandardOutput = true; 
     psi.RedirectStandardError = true; 
     psi.RedirectStandardInput = true; 
     psi.CreateNoWindow = true; 
     p.StartInfo = psi; 
     p.Start(); 

这将启动一个子进程,而不显示控制台窗口,将允许StandardOutput的捕捉等

+0

你的答案是方式更丰富,然后我的+1 – 2009-01-12 20:51:13

-1

我们在过去通过以编程方式使用命令行执行我们的过程来完成此操作。

5

签入ProcessStartInfo并设置WindowStyle = ProcessWindowStyle.Hidden和CreateNoWindow = true。

+1

对于控制台应用程序,我发现您只需要** WindowStyle = ProcessWindowStyle.Hidden **。你不需要** CreateNoWindow = true **。 – 2010-08-03 06:25:15

1

如果你想获取过程在执行过程中的输出,您可以执行以下操作(示例使用'ping'命令):

var info = new ProcessStartInfo("ping", "stackoverflow.com") { 
    UseShellExecute = false, 
    RedirectStandardOutput = true, 
    CreateNoWindow = true 
}; 
var cmd = new Process() { StartInfo = info }; 
cmd.Start(); 
var so = cmd.StandardOutput; 
while(!so.EndOfStream) { 
    var c = ((char)so.Read()); // or so.ReadLine(), etc 
    Console.Write(c); // or whatever you want 
} 
... 
cmd.Dispose(); // Don't forget, or else wrap in a using statement 
相关问题