1

我想启动一个 cmd 文件并将输出重定向到控制台,但它不起作用。我必须做什么?我已经设置了 startInfo.RedirectStandardOutput = true

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();
4

2 回答 2

1

来自MSDN

startInfo.UseShellExecute = false;

然后你可以得到输出

Console.WriteLine(process.StandardOutput.ReadToEnd());
于 2013-04-09T11:02:18.880 回答
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());
于 2013-04-10T00:21:28.610 回答