C# output of running command prompt
- by Kaushal Singh
In this following code whenever I send any command like dir or anything this function get stuck in while loop...
I want the output of the command prompt each time I send it command to execute without closing the process after the execution of command.
public void shell(String cmd)
{
ProcessStartInfo PSI = new ProcessStartInfo();
PSI.FileName = "c:\\windows\\system32\\cmd.exe";
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
Process p = Process.Start(PSI);
StreamWriter CSW = p.StandardInput;
StreamReader CSR = p.StandardOutput;
CSW.WriteLine(cmd);
CSW.Flush();
while(!(CSR.EndOfStream))
{
Console.WriteLine(CSR.ReadLine());
}
CSR.Close();
}