another solution:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_CmdExec
{
class Program
{
static void Main(string[] args)
{
try
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = @"d:\"
};
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;
p.Start();
p.StandardInput.Write("dir" + p.StandardInput.NewLine);
p.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Process p = sender as Process;
if (p == null)
return;
Console.WriteLine(e.Data);
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Process p = sender as Process;
if (p == null)
return;
Console.WriteLine(e.Data);
}
}
}