Reputation: 14593
After looking on forums, I have written this snippet:
public string ExecuteCmd()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = this.m_command;
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
The m_command
is member of a class, initialized in the constructor.
For my tests, it is net user
. When the compiler arrives at this point, I get the following exception:
StandardOut has not been redirected or the process hasn't started yet.
Where is my mistake?
Upvotes: 0
Views: 77
Reputation: 63327
You need this:
//....
startInfo.Arguments = "/C " + this.m_command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//....
Upvotes: 1