2009-01-12 111 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 
相關問題