I am trying to write a code which would run a command from command prompt and show the output of the command. Basically the command would check the status of a window service if its running or stopped. Here is the code:
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"cmd.exe";
startInfo.Arguments = "sc query \"My Service\" | findstr STATE";
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
}
If I run the command on command line:
sc query "My Service" | findstr STATE
This returns me:
STATE : 1 STOPPED
But if I run my c# code above, nothing happens and after some time my browser throws error:
HTTP Error 502.3 - Bad Gateway The specified CGI application encountered an error and the server terminated the process.
Not sure what else is missing here.