2013-04-09 48 views
1

我想啓動一個cmd文件並將輸出重定向到控制檯,但它不起作用。我必須做什麼?我已經設置了startInfo.RedirectStandardOutput = true如何重定向cmnd輸出

com = "Parameter"; 

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
System.Diagnostics.ProcessStartInfo startInfo = 
    new System.Diagnostics.ProcessStartInfo(); 

startInfo.FileName = Properties.Settings.Default.pathTo+ @"\Make\copy.cmd"; 
startInfo.Arguments = com; 
startInfo.RedirectStandardOutput = true; 
startInfo.UseShellExecute = false; 
process.StartInfo = startInfo; 
process.Start(); 
output = process.StandardOutput; 
string ln; 

while ((ln = output.ReadLine()) != null) 
{ 
    Console.WriteLine(line); 
} 
process.WaitForExit(); 
+0

可能DUP授權的http://stackoverflow.com/questions/3829749/how-to-redirect-process-output-to-system-string?rq=1和http://stackoverflow.com/questions/285760/how-to-它產生了一個進程並捕獲它的stdout-in-net?rq = 1 – Matten 2013-04-09 11:00:26

回答

1

MSDN

startInfo.UseShellExecute = false;

然後你就可以得到輸出

Console.WriteLine(process.StandardOutput.ReadToEnd()); 
+0

它不起作用。我收到一個錯誤。你不能設置startInfo.UseShellExecute = true;和startInfo.RedirectStandardOutput = true; – tux007 2013-04-09 23:57:42

+0

對不起 - 我的不好 - ShellExecute應該設置爲false重定向STD輸出。 – 2013-04-10 02:48:02

0

現在,我有這樣的解決方案:

   com = "Parameter";; 

       System.Diagnostics.Process process = new System.Diagnostics.Process(); 
       System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
       startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copy.cmd"; 
       startInfo.Arguments = com; 
       startInfo.RedirectStandardOutput = true; 
       startInfo.RedirectStandardError = true; 
       startInfo.UseShellExecute = false; 
       startInfo.CreateNoWindow = true; 
       process.StartInfo = startInfo; 
       process.Start(); 
       process.WaitForExit(); 
       Console.WriteLine(process.StandardOutput.ReadToEnd()); 
       Console.WriteLine(process.StandardError.ReadToEnd());