C# 编写命令行交互工具——实时输出_获取执行结果

咱们在写程序的时候一般会用到命令行工具。前端

如Ping 某个网段,写个注册表,启动项,或者感谢其余坏事。shell

在网上查了一下,多数都说用C# 作命令行交互须要作不少不少的逻辑处理。那么今天博主也来写一个简单一点的。c#

首先咱们建一个CmdUtils类,而后编写咱们须要的方法工具

那么在开始以前,咱们先看下网上提供的方法。this



那么开始,咱们先新建一个类,而后编写方法命令行


Process cmd = null;
            if (cmd == null)
            {
                cmd = new Process();//建立进程对象  
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";//设定须要执行的命令  
                startInfo.Arguments = "/C "+shell;//“/C”表示执行完命令后立刻退出  
                startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                startInfo.RedirectStandardInput = true;//不重定向输入  
                startInfo.RedirectStandardOutput = true; //重定向输出  
                startInfo.CreateNoWindow = true;//不建立窗口  
                cmd.StartInfo = startInfo;
               // cmd.Start();
            }
            if (cmd.Start())//开始进程  
            {
                string str = cmd.StandardOutput.ReadToEnd();
                cmd.Close();

                cmd = null;
                return str;
            }
            return null;

那么很明显,cmd 在执行完毕以后就被关闭了线程

Ok,修改一下,不加/c 也就是执行完毕以后不退出code

Process cmd = null;
             if (cmd == null)
             {
                 cmd = new Process();//建立进程对象  
                 ProcessStartInfo startInfo = new ProcessStartInfo();
                 startInfo.FileName = "cmd.exe";//设定须要执行的命令  
                 startInfo.Arguments = "";//“/C”表示执行完命令后立刻退出  
                 startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                 startInfo.RedirectStandardInput = true;//不重定向输入  
                 startInfo.RedirectStandardOutput = true; //重定向输出  
                 startInfo.CreateNoWindow = true;//不建立窗口  
                 cmd.StartInfo = startInfo;
                 // cmd.Start();
             }

这样子就能够打开以后不关闭了。

下面接着写怎样将前端显示的搞到执行的来。咱们都知道string 属于引用数据类型,那么笨方法就从该变量下手。orm

咱们在CmdUtils中定义一个shell 的string 变量,并设置访问权限为public ,方便调用。对象

public String shell = "";

那么界面上应该怎样显示呢。


public partial class Cmd : Form
    {
        public String isRun ="start";
        CmdUtils cmd = new CmdUtils();
        public Cmd()
        {
            InitializeComponent();
            new Thread(new ThreadStart(init)).Start();
        }
        private void init() {
            cmd.sendCmd(this);
        }

        private void sendBtn_Click(object sender, EventArgs e)
        {
            cmd.shell = comEdit.Text;
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            cmd.shell = "exit";
        }
        

      
    }

咱们首先启动一个线程,而后经过改变shell 变量达到交互效果。

下面上CmdUtils 代码

public class CmdUtils
    {
         public String shell = "";
         public void sendCmd(Cmd cmdoom) {
             Process cmd = null;
             if (cmd == null)
             {
                 cmd = new Process();//建立进程对象  
                 ProcessStartInfo startInfo = new ProcessStartInfo();
                 startInfo.FileName = "cmd.exe";//设定须要执行的命令  
                 startInfo.Arguments = "";//“/C”表示执行完命令后立刻退出  
                 startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                 startInfo.RedirectStandardInput = true;//不重定向输入  
                 startInfo.RedirectStandardOutput = true; //重定向输出  
                 startInfo.CreateNoWindow = true;//不建立窗口  
                 cmd.StartInfo = startInfo;
                 // cmd.Start();
             }
             if (cmd.Start())//开始进程  
             {
                 cmd.StandardOutput.ReadLine().Trim();
                 cmd.StandardOutput.ReadLine().Trim();
                 while (cmdoom.isRun.IndexOf("start") != -1) {
                     if (shell.Length > 0) { 
                        cmd.StandardInput.WriteLine(shell);
                        cmd.StandardOutput.ReadLine().Trim();

                        cmd.StandardInput.WriteLine("\n");
                        String log = cmd.StandardOutput.ReadLine().Trim();
                        String path = log.Substring(0, 2).ToUpper();
                        updateLog(cmdoom, log);
                        log = "";
                        do
                        {
                            
                            String logm = cmd.StandardOutput.ReadLine().Trim();
                            if (logm.IndexOf(path) != -1){
                            
                                break;
                            }
                            updateLog(cmdoom, logm+"\n");
                            log+=logm;
                            
                        } while (true);
                        
                        shell = "";
                     }
                 }
                 
                 cmd.Close();

                 cmd = null;
                 return ;
             }
             return ;
         }
         private delegate void UpdateLog();

         private void updateLog(Cmd cmd, String log) {
             UpdateLog set = delegate()
             {
                 cmd.cmdLogTextArea.AppendText("\n" + log);
             };
             cmd.Invoke(set);
         }
    }

那么最后的效果图为

相关文章
相关标签/搜索