C#-执行cmd命令,获取结果

摘要:
=null){MessageBox.Show("只能运行一个程序!

C#-执行cmd命令,获取结果第1张

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EFDemo {
    public partial class ExecCmd : Form {
        public ExecCmd() {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e) {
            if (pro == null) { MessageBox.Show("进程未开启"); return; }
            Task.Factory.StartNew(() => {

                AppendLog("执行命令: " + textBox1.Text);
                pro.StandardInput.WriteLine(textBox1.Text);
                //pro.StandardInput.WriteLine("exit");
                pro.StandardInput.AutoFlush = true;
                //获取cmd窗口的输出信息

                string output = "";
                while (true) {
                    output = pro.StandardOutput.ReadLine();
                    AppendLog(output);
                }
            });
        }

        public void AppendLog(string str) {
            textBox2.AppendText("
" + str);
        }


        //CMD进程
        System.Diagnostics.Process pro = null;
        //启动线程
        private void button2_Click(object sender, EventArgs e) {

            if (pro != null) {
                MessageBox.Show("只能运行一个程序!", "请确定", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            AppendLog("初始化参数");
            pro = new System.Diagnostics.Process();
            pro.StartInfo.FileName = "cmd.exe";
            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.RedirectStandardError = true;
            pro.StartInfo.RedirectStandardInput = true;
            pro.StartInfo.RedirectStandardOutput = true;
            pro.StartInfo.CreateNoWindow = true;
            //pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            AppendLog("启动");
            pro.Start();
        }

        //关闭
        private void button3_Click(object sender, EventArgs e) {
            if (pro != null) {
                pro.Close();
                pro = null;
                AppendLog("关闭");
            }
        }



    }
}

免责声明:文章转载自《C#-执行cmd命令,获取结果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HDU 4417 主席树写法C# 与 SQLite的操作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#连接Oracle数据库的方法(Oracle.DataAccess.Client也叫ODP.net)

官方下载地址(ODP.net)(中文):http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/index.html 官方下载地址(ODP.net):http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html 首先...

Android Verified Boot 2.0简要

from: https://ressrc.com/2018/10/26/android-verified-boot-2-0%E7%AE%80%E8%A6%81-thanksgining/   简要 AVB2.0被用于启动引导,此用法添加一个“vbmeta.img”镜像。public key被编译到bootloader中用于校验vbmeta数据,vb...

捕捉JDialog的关闭事件

捕捉JDialog的关闭事件 http://xxqn.iteye.com/blog/431190 publicclassEditJDialogextendsjavax.swing.JDialogimplementsWindowListener{ publicEditJDialog(Frameowner,booleanmodal){ super(own...

生成uuid 和 检验

//注意replaceAll前面的是正则表达式 String uuid = UUID.randomUUID().toString().replaceAll("-",""); System.out.println(uuid);// System.out.println(uuid.length()); 时间戳:System.currentTimeMillis(...

oracle 数据库备份 cmd 命令

语句:exp SDE/SDE@IP:1521/orcl FILE=C:数据库备份sde%date:~0,4%%date:~5,2%%date:~8,2%.dmp  COMPRESS=y direct=y log=C:数据库备份logsde%date:~0,4%%date:~5,2%%date:~8,2%.log 在这里主要要保证C盘下有数据库备份和数据库备...

用navicate 连接本地数据库提示用户名/口令无效

1、在用navicate连接本地的oracle数据库时,试了oracle几个默认的用户名和密码,但是当我输入时,却提示用户名/口令无效。所以按照网上的办法,cmd,输入了以下命令,修改了几个用户的用户密码。 再次用admin连接即可 2、文件--新建--用户,建立admin用户 3、在新建的表里插入数据时,提示:对表空间‘USERS’无权限。 解决办法...