C# 执行CMD 命令

一曲冷凌霜 提交于 2019-11-28 12:25:59
Process prc = new Process();
prc.StartInfo.FileName = "cmd.exe";
//关闭 Shell 的使用!
prc.StartInfo.UseShellExecute = false;
//重定向标准输入
prc.StartInfo.RedirectStandardInput = true;
//重定向标准输出
prc.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
prc.StartInfo.RedirectStandardError = true;
//设置不显示窗口!
prc.StartInfo.CreateNoWindow = true;
//执行doc 命令
prc.Start();
prc.StandardInput.WriteLine("Dir C:\\"); //输入命 并回车,因是WriteLine,不是Write
prc.StandardInput.WriteLine("exit");//cmd.exe 要退出了
//从输出流中 取得 执行后的结果
string sResult = prc.StandardOutput.ReadToEnd();
MessageBox.Show(sResult,"提示",MessageBoxButtons.OK);

  

编辑器加载中...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!