c# execute cmd command and get output code example

Example: how to execute command line in c# and get response

using (Process p = new Process())
{
	// set start info
    p.StartInfo = new ProcessStartInfo("cmd.exe")
    {
    	RedirectStandardInput = true,
        UseShellExecute = false,
        WorkingDirectory = @"C:\"
    };
    // event handlers for output & error
    p.OutputDataReceived += p_OutputDataReceived;
    p.ErrorDataReceived += p_ErrorDataReceived;

    // start process
    p.Start();
    // send command to its input
    p.StandardInput.Write("dir" + p.StandardInput.NewLine);
    //wait
    p.WaitForExit();
}