c# 调用CMD窗口执行命令( 阻塞执行;防阻塞+异步回显)

转载:https://blog.csdn.net/fangyu723/article/details/118783975

1.调用CMD窗体执行命令 阻塞执行, 并在最后执行完后一次性输出执行结果

        public static string RunCmd(string cmd)
        {
            //string strInput = Console.ReadLine();
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            //启动程序
            p.Start();
 
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(cmd + "&exit");
 
            p.StandardInput.AutoFlush = true;
 
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            string strErrOuput = p.StandardError.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
            return strOuput;
            //Console.WriteLine(strOuput);
        }

2.调用CMD窗体执行命令 实时获取执行结果并输出(防阻塞+异步回显)

        //异步回显(1-2-3-4)
        // 1.定义委托
        public delegate void DelReadStdOutput(string result);
        public delegate void DelReadErrOutput(string result);
        // 2.定义委托事件
        public event DelReadStdOutput ReadStdOutput;
        public event DelReadErrOutput ReadErrOutput;
 
        public string RunCmd(string cmd)
        {
 
            //3.将相应函数注册到委托事件中
            ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);
            ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);
 
            try
            {
                //string strInput = Console.ReadLine();
                Process p = new Process();
                //设置要启动的应用程序
                p.StartInfo.FileName = "cmd.exe";
                //是否使用操作系统shell启动
                p.StartInfo.UseShellExecute = false;
                // 接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardInput = true;
                //输出信息
                p.StartInfo.RedirectStandardOutput = true;
                // 输出错误
                p.StartInfo.RedirectStandardError = true;
                //不显示程序窗口
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
 
                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
                //启用Exited事件
                p.EnableRaisingEvents = true;
                p.Exited += new EventHandler(Process_Exited);
 
                //启动程序
                p.Start();
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();
                p.StandardInput.AutoFlush = true;
                //输入命令
                p.StandardInput.WriteLine(cmd);
                p.StandardInput.WriteLine("exit");
 
                //获取输出信息
                // string strOuput = p.StandardOutput.ReadToEnd();
                //等待程序执行完退出进程
                //p.WaitForExit();
                //p.Close();
                // return strOuput;
                return string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
 
        private void ReadStdOutputAction(string result)
        {
            //this.textBoxShowStdRet.AppendText(result + "\r\n");
        }
 
        private void ReadErrOutputAction(string result)
        {
            //this.textBoxShowErrRet.AppendText(result + "\r\n");
        }
 
        private void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                // 4. 异步调用,需要invoke
                this.Invoke(ReadStdOutput, new object[] { e.Data });
                Console.WriteLine(e.Data);
            }
        }
 
        private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                this.Invoke(ReadErrOutput, new object[] { e.Data });
                Console.WriteLine(e.Data);
            }
        }
 
        private void Process_Exited(object sender, EventArgs e)
        { Console.WriteLine("命令执行完毕"); }
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值