c# run a cmd commands and get output code example
Example 1: how to make c# program run cmd commands
string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Example 2: how to execute command line in c# and get response
using (Process p = new Process())
{
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);
p.WaitForExit();
}