C#执行外部程序之执行DOS命令

2019-04-03  本文已影响0人  flyinghat
ReturnModel 是自定义返回格式类

鉴于某些用户的电脑系统安装不是C盘,因此需要用下面代码找到cmd.exe

   private static string CMDPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) + "\\cmd.exe";
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="cmd">执行命令</param>
        /// <param name="totalMilliseconds"></param>
        /// <returns></returns>
        public static ReturnModel RunCmd(string cmd, out double totalMilliseconds)
        {
            totalMilliseconds = 0D;
            var outSuccess = string.Empty;
            var outFail = string.Empty;
            if (!string.IsNullOrWhiteSpace(cmd))
            {
                Process proc = new Process();//创建进程对象  
                proc.StartInfo.FileName = "cmd.exe";//设定需要执行的命令  
                proc.StartInfo.Arguments = "/C " + cmd;//“/C”表示执行完命令后马上退出  
                proc.StartInfo.UseShellExecute = false;//不使用系统外壳程序启动 
                proc.StartInfo.RedirectStandardInput = false;//不重定向输入  
                proc.StartInfo.RedirectStandardOutput = true; //重定向输出  
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;//不创建窗口  
                try
                {
                    if (proc.Start())
                    {
                        proc.WaitForExit(100);//等待进程结束,等待时间为指定的毫秒
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        outSuccess = proc.StandardOutput.ReadToEnd();//读取进程的输出 
                        outFail = proc.StandardError.ReadToEnd();
                        watch.Stop();
                        totalMilliseconds = watch.Elapsed.TotalMilliseconds;
                        OnlineData.Helper.HelperLog.Info(string.Format("成功{0},失败{1}", outSuccess, outFail));
                        return string.IsNullOrWhiteSpace(outFail) ? ReturnModel.Success(data: outSuccess) : ReturnModel.Fail(data: outFail);
                    }
                }
                catch (Exception ex)
                {
                    outFail = proc.StandardError.ReadToEnd();
                    OnlineData.Helper.HelperLog.Info(string.Format("成功{0},失败{1}", outSuccess, outFail), ex);
                    return ReturnModel.Fail(data: outFail);
                }
                finally
                {
                    if (!proc.HasExited)
                    {
                        //测试进程是否还有响应
                        if (!proc.Responding)
                        {
                            //立即停止相关进程。意即,进程没回应,强制关闭
                            proc.Kill();
                        }
                    }
                    if (proc != null)
                    {
                        proc.Close();
                        proc.Dispose();
                        proc = null;
                    }
                }
            }
            return ReturnModel.Fail(data: "命令为空");
        }
上一篇下一篇

猜你喜欢

热点阅读