我怎么能告诉程序“test.exe”(这是一个控制台应用程序)将其结果输出到一个文件。
例如,通常程序可以通过在提示符处执行test.exe> output.txt来输出数据。
这个说法怎么可能呢?
Process.Start("test.exe", "\"" + exename + "\"").WaitForExit();
如果您只想将输出重定向到一个名为“管道”的文件,则可以要求cmd.exe为您执行此操作。 即
Process.Start("cmd.exe", "/c test.exe \"" + exename + "\" > D:\\testOutput.txt").WaitForExit();
从MSDN页面中使用此示例中的StandardOutput
属性:
// Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "Write500Lines.exe"; p.Start(); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();
正如你所看到的,你需要设置Process
的StartInfo
的RedirectStandardOutput
属性。
您可以在代码中将标准输出重定向到FileStream 。