Log to text file from Process.Start
-
11-02-2021 - |
Frage
I am starting a process using Process.Start(ProcessStartInfo). It currently brings up a console window and the output of the process is displayed there until the process completes, in which case the console window closes automatically.
The process outputs a lot of text, so I do not just want to redirect this output to a string, like examples I have found so far.
How can I get the text of the console output to go into a text log file?
ProcessStartInfo myPSI = new ProcessStartInfo();
myPSI.FileName = myFileName;
myPSI.Arguments = myArgs;
myPSI.CreateNoWindow = false;
myPSI.UseShellExecute = false;
myPSI.WindowStyle = ProcessWindowStyle.Hidden;
try
{
using (Process exeProcess = Process.Start(myPSI))
{
exeProcess.WaitForExit();
}
}
catch
{
}
Lösung
You can redirect the output to whatever you want... for example a stream... you can even process the output in a separate thread if you want to - for source code and details see http://www.codeproject.com/KB/threads/ReadProcessStdoutStderr.aspx
Andere Tipps
You need to use output redirection. See here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx