C#实现cmd命令的相同效果

自己写的,有效

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string cmd = @"system info";
                ExcuteCmd(cmd);           
            }
            catch (System.Exception ex)
            {

            }
        }
        
        /// <summary>
        /// 执行Cmd命令
        /// </summary>
        public string ExcuteCmd(string c, string workDirectory=null)
        {
            //创建执行cmd
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            //process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";

            //process.StartInfo.WorkingDirectory = workDirectory;
            string cmdPath = @"c:\Users\vi.ta";//关键这里要跳到cmd执行命令的这里,cmd窗口使用的目录
            if(textBoxPath.Text=="")
            {
                MessageBox.Show("请输入要执行的命令的目录");
                //return null;//这里加了一个判断,根据不同用户的目录不同,输入'
            }
            else
            {
                cmdPath = textBoxPath.Text;
            }
            process.StartInfo.WorkingDirectory = cmdPath;//这个地方决定了运行的路径
            //p.startinfo.arguments="cd " + Application.StartPath;//改变路径

            //配置开发方式输入输出错误
            process.StartInfo.UseShellExecute = false;//不启动shell启动程序
            process.StartInfo.CreateNoWindow = true;//不创建新窗口
            process.StartInfo.RedirectStandardOutput = true;//重定向错误输出
            process.StartInfo.RedirectStandardInput = true;//重定向输入

            //执行cmd且获取返回值
            process.Start();

            
            //当程序输入python时,系统进入到python中,所以没有反应
            process.StandardInput.WriteLine(c);//向cmd窗口发送信息 ,后面+&exit表明运行后马上退出
            process.StandardInput.AutoFlush = true;//提交
            process.StandardInput.WriteLine("exit");


            //读取返回值
            StreamReader reader = process.StandardOutput;//截取输出流

            string output = reader.ReadLine();//每次读取一行

            while (!reader.EndOfStream)
            {
                //PrintThrendInfo(output);
                output += reader.ReadLine();
                PrintMsg(output);
            }

            process.WaitForExit();//等待程序进行完退出程序
            return output;  //输出返回值
        }


    


        /// <summary>
        /// 打印信息至文本框
        /// </summary>
        /// <param name="msg">要打印的信息</param>
        /// <param name="flag">true则在信息末尾打印回车,false不打印回车</param>
        public  void  PrintMsg(string msg, bool flag = true)
        {
            try
            {
                this.richTextBox_ReceMsg.BeginInvoke((Action)delegate
                {
                    //如果RichTextBox已接受文本字符超过50000个字符则清空
                    if (richTextBox_ReceMsg.Text.Length >= 100000)//richTextBox_ReceMsg.MaxLength/50000
                    {
                        richTextBox_ReceMsg.Clear();
                    }
                    if (flag)
                    {
                        richTextBox_ReceMsg.AppendText(msg + "\n");
                    }
                    else
                        richTextBox_ReceMsg.AppendText(msg);

                    //光标滚动到最后一行
                    this.richTextBox_ReceMsg.ScrollToCaret();
                });
            }
            catch (System.Exception ex)
            {
                richTextBox_ReceMsg.AppendText(ex.Message + "\n");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string cmd = @"ifconfig/all";
                ExcuteCmd(cmd);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("测试TestThumbstick up 失败" + ex.Message);
            }
        }


后面的来自网上转载


C#两种处理cmd的方式


        public  string startcmdOne(string command)
        {
            string output = "";
            try
            {
 
                Process cmd = new Process();
                cmd.StartInfo.FileName = command;
 
                cmd.StartInfo.UseShellExecute = false; 
 
                cmd.StartInfo.RedirectStandardInput = true; 
                cmd.StartInfo.RedirectStandardOutput = true;
 
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 
                cmd.Start(); 
 
                output = cmd.StandardOutput.ReadToEnd();
                PrintMsg(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("startcmdOne error" + e.Message);
            }
            return output;
        }

        public  string startcmdTwo(string command, string argument)
        {
            string output = "";
            try
            {
                Process cmd = new Process();
 
                cmd.StartInfo.FileName = command;
                cmd.StartInfo.Arguments = argument;
 
                cmd.StartInfo.UseShellExecute = false; 
 
                cmd.StartInfo.RedirectStandardInput = true; 
                cmd.StartInfo.RedirectStandardOutput = true; 
 
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 
                cmd.Start(); 
 
                output = cmd.StandardOutput.ReadToEnd();
                PrintMsg(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("startcmdTwo error" + e.Message);
            }
            return output;
        }


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚努力的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值