Adding the ">" to a Process.Start instance
How could I tell the program "test.exe" (which is a console application) to output its result to a file.
E.g. usually the program can output the data by doing test.exe > output.txt at the prompt.
How could this be done in this statement?
Process.Start("test.exe", "\"" + exen开发者_运维问答ame + "\"").WaitForExit();
Use the StandardOutput
property as in this example from the MSDN page:
// 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();
As you can see you need to set the RedirectStandardOutput
property of the Process
's StartInfo
.
and if you only want to redirect the output to a file, called piping, you can ask cmd.exe to do that for you. i.e.
Process.Start("cmd.exe", "/c test.exe \"" + exename + "\" > D:\\testOutput.txt").WaitForExit();
You can, in code, redirect the standard output to a FileStream.
精彩评论