110,569
社区成员
发帖
与我相关
我的任务
分享
public class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "cmd";
string strOut = string.Empty;
string strError = string.Empty;
p.OutputDataReceived += (o, e) => { strOut += e.Data + "\n"; };
p.ErrorDataReceived += (o, e) => { strError += e.Data + "\n"; };
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.StandardInput.WriteLine("dir");
p.WaitForExit(1000);
Console.WriteLine(strOut);
Console.WriteLine(strError);
p.StandardInput.WriteLine("exit");
p.WaitForExit();
}
}