c#调用本地命令并截取Output

简介: demo1: /// /// /// /// /// 是否是追加 private void ShowOutput(string str,bool append) { if (this.
demo1:
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="append">是否是追加</param>
        private void ShowOutput(string str,bool append) {
            if (this.txtOutput.InvokeRequired)
            {
                this.txtOutput.Invoke(new MethodInvoker(() => {
                    if (append)
                    {
                        this.txtOutput.AppendText(str);
                        this.txtOutput.AppendText(System.Environment.NewLine);
                    }
                    else
                    {
                        this.txtOutput.Clear();
                    }
                    
                }));
            }
            else
            {
                if (append)
                {
                    this.txtOutput.AppendText(str);
                    this.txtOutput.AppendText(System.Environment.NewLine);
                }
                else
                {
                    this.txtOutput.Clear();
                }
            }
        }

        



        private void btnRun_Click(object sender, EventArgs e)
        {

            Thread thread = new Thread(() => {

                ShowOutput("",false);
                //this.txtOutput.Clear();
                ProcessStartInfo psi = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                psi.Arguments = this.txtMachine.Text;//设置命令参数
                psi.CreateNoWindow = true;//不显示dos命令行窗口
                psi.RedirectStandardOutput = true;//
                psi.RedirectStandardInput = true;//
                psi.UseShellExecute = false;//是否指定操作系统外壳进程启动程序

                Process p = Process.Start(psi);
                StreamReader reader = p.StandardOutput;//截取输出流
                string line = reader.ReadLine();//每次读取一行
                while (!reader.EndOfStream)
                {
                    ;
                    ShowOutput(line,true);
                    line = reader.ReadLine();
                }
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();//关闭进程
                reader.Close();//关闭流
            
            });

            thread.IsBackground = true;
            thread.Start();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.txtMachine.Text = "127.0.0.1";
        }

 

 

 

demo2:

        /// <summary>
        /// appoint exe
        /// </summary>
        /// <param name="exeStr"></param>
        /// <param name="fileStr"></param>
        public void OpenFile(string exeStr,string fileStr) { 
            //ProcessStartInfo
            ProcessStartInfo psi;
            if (String.IsNullOrEmpty(exeStr))
            {
                psi = new ProcessStartInfo();
            }
            else
            {
                psi = new ProcessStartInfo(exeStr);//应用程序或文档名
            }
           
            psi.Arguments = fileStr;

            Process process = new Process();
            process.StartInfo = psi;
            process.Start();

        }


        public void OpenFile(string fileStr)
        {
            OpenFile(null, fileStr);
        }

 

 

demo3:

        public static void PintTest()
        {
            //Allows an application to determine whether a remote computer is accessible over the network.
            Ping pingSender = new Ping();

            // Create a buffer of 32 bytes of data to be transmitted. 
            string data = "sendMsg";
            byte[] buffer = Encoding.ASCII.GetBytes(data);

            // Wait 10 seconds for a reply. 
            int timeout = 10000;

            // Set options for transmission: 
            // The data can go through 64 gateways or routers 
            // before it is destroyed, and the data packet 
            // cannot be fragmented.
            PingOptions options = new PingOptions(64, true);

            // Send the request.
            PingReply reply = pingSender.Send("www.baidu.com", timeout, buffer, options);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address: {0}", reply.Address.ToString());
                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                //A Byte array containing the data received in an ICMP echo reply message, or an empty array, if no reply was received.
                Console.WriteLine("Received in an ICMP echo reply message: {0}", Encoding.Default.GetString(reply.Buffer));
            }
            else
            {
                Console.WriteLine(reply.Status);
            }
        }
相关文章
|
6月前
|
C#
命令调用C#程序, 路径参数解析错误
命令调用C#程序, 路径参数解析错误
|
9月前
|
C#
C#利用委托实现命令按钮跨窗体控制
C#利用委托实现命令按钮跨窗体控制
78 0
|
存储 JavaScript 前端开发
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
110 0
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
|
前端开发 C# 数据格式
C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码
原文:C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码 前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.
1215 0
|
C#
【C#/WPF】图像变换的Undo撤销——用Stack命令栈
原文:【C#/WPF】图像变换的Undo撤销——用Stack命令栈 需求: 图层中有一张图片,可以对该图层进行平移、缩放、旋转操作,现在要求做Undo撤销功能,使得图层回复上一步操作时的状态。
842 0
|
C#
使用C# (.NET Core) 实现命令设计模式 (Command Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有这样一个可编程的新型遥控器, 它有7个可编程插槽, 每个插槽可连接不同的家用电器设备. 每个插槽对应两个按钮: 开, 关(ON, OFF).
820 0