C# output of running command prompt

Posted by Kaushal Singh on Programmers See other posts from Programmers or by Kaushal Singh
Published on 2012-10-10T03:42:21Z Indexed on 2012/10/10 3:52 UTC
Read the original article Hit count: 358

Filed under:

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();

}

© Programmers or respective owner

Related posts about c#