Using New Process() in C#, how can I copy the command line text to a text file?
-
14-04-2021 - |
Frage
I want to run lmutil.exe with the arguments -a, -c, and 3400@takd, then put everything that command line prompt generates into a text file. What I have below isn't working.
If I step through the process, I get errors like "threw an exception of type System.InvalidOperationException"
Process p = new Process();
p.StartInfo.FileName = @"C:\FlexLM\lmutil.exe";
p.StartInfo.Arguments = "lmstat -a -c 3400@tkad>Report.txt";
p.Start();
p.WaitForExit();
All I want is for the command line output to be written to Report.txt
Lösung
To get the Process
output you can use the StandardOutput
property documented here.
Then you can write it to a file:
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = @"C:\FlexLM\lmutil.exe";
p.StartInfo.Arguments = "lmstat -a -c 3400@tkad";
p.Start();
System.IO.File.WriteAllText("Report.txt", p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
Andere Tipps
You can't use >
to redirect via Process, you have to use StandardOutput
. Also note that for it to work StartInfo.RedirectStandardOutput
has to be set to true.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow