添加“>";到进程.启动实例
本文关键字:进程 启动 实例 quot gt 添加 | 更新日期: 2023-09-27 17:59:43
如何告诉程序"test.exe"(它是一个控制台应用程序)将其结果输出到文件中。
例如,通常程序可以通过在提示下执行test.exe>output.txt来输出数据。
如何在这份声明中做到这一点?
Process.Start("test.exe", "'"" + exename + "'"").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
属性。
如果您只想将输出重定向到一个名为piping的文件,您可以要求cmd.exe为您执行此操作。即
Process.Start("cmd.exe", "/c test.exe '"" + exename + "'" > D:''testOutput.txt").WaitForExit();
您可以在代码中将标准输出重定向到FileStream。