执行cmd程序

public static string RunCommand(string cmd)
        {
            try
            {
                //创建一个进程
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.Start();//启动程序
                p.StandardInput.WriteLine(cmd + "&exit");
                p.StandardInput.AutoFlush = true;
                string output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
                return output;
            }
            catch (Exception Exp)
            {
                Log4NetHelper.WriteLog("LTSW.GeneralMethod--CommandExecute--RunCommand", Exp);
                return Exp.Message;
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_43237948/article/details/86133911