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

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);
            }
        }
时间: 2024-09-13 12:43:22

c#调用本地命令并截取Output的相关文章

C#调用DOS命令方法

C#调用DOS命令我想编一个汇编语言编辑器,但在调用masm.exe的时候,无法得到它的运行信息.代码如下: Process p = new Process(); p.StartInfo .WorkingDirectory ="e:\\"; p.StartInfo.FileName = "masm.exe"; p.StartInfo.Arguments="test.asm;"; p.StartInfo.UseShellExecute = fals

网页中调用本地应用程序时的路径问题

由于一些原因,需要网页中调用本地应用程序,在网上查了一下,有好几种方法,有的是用javascript实现的,这种方法有安全限制,并且只能用IE浏览器,还有一种方法比较好,就是添加自定义URL协议,这种方法必须先在客户端将应用程序添加成为URL协议,上面两种方法的具体操作步骤网上都有. 下面说一下"添加自定义URL协议"这种方法的一个问题,配置完成后,可以正常调用本地应用程序,但是,这个应用程序在登录时又调用了另外一个本地应用,这时,就会弹出"系统找不到文件"之类的错

tomcat-java 调用其它命令窗口bat并输入命令

问题描述 java 调用其它命令窗口bat并输入命令 用 jave 调用 cmd 打开 Tomcat 的start.bat. 并向Tomcat这个 命令窗口 输入命令执行(即在jave 中完成 手动在Tomcat输入命令的过程,实现自动化操作 ). 现在问题是可以通过 running.exec 打开 start.bat ,也可以 获得这个bat 启动时的输出. 但现在问题是,当我用 output stream 输入 命令到这个 窗口时,却没有反应. 请高手赐教,很急哦. 谢谢 解决方案 谢谢 ,

脚本-写火狐扩展,调用本地的vbs文件,报错错误:0x800700c1代码:800700c1源(null)

问题描述 写火狐扩展,调用本地的vbs文件,报错错误:0x800700c1代码:800700c1源(null) 写火狐扩展,调用浏览器本地的vbs文件,报错错误:0x800700c1 代码:800700c1 源(null),但是直接鼠标双击打开正常,vbs文件的内容如下: createobject("wscript.shell").run "tortoiseProcScript.bat",0,true wscript 和cscript2种打开方式都试过,例如 用&q

利用html5调用本地摄像头拍照上传图片

原文:利用html5调用本地摄像头拍照上传图片 xmlns="http://www.w3.org/1999/xhtml">    html5概念啥的就不废话了,不知道的 百度, 谷歌一堆..今天学了学html5中的Canvas结合新增的<video>标签来获取本地摄像头,在html5之前,要在浏览器获取本地摄像头只有通过插件(ActiveX,但是这种只有IE支持)或者是flash来获取(或许你没学过flash那就很坑爹了),在之后微软的silvertlight中也可以

7.使用ProcessBuilder执行本地命令(转)

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.ut

PHP调用linux命令详细说明

PHP调用linux命令详细说明 /* 在php教程中调用linux命令的函数是 string   exec(string   command,   string   [array],   int   [return_var]); 如 exec( "vpopmail "); echo exec('whoami'); 再看一实例 function exec_enabled() {   $disabled = explode(', ', ini_get('disable_functions

在网页中调用本地的应用程序(转自CSDN)

在网页中调用本地的应用程序例子: <script>function exec (command) {    window.oldOnError = window.onerror;    window._command = command;    window.onerror = function (err) {        if (err.indexOf('utomation') != -1) {        alert('命令' + window._command + ' 已经被用户禁止

python 调用 shell 命令方法

python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等  例:a=os.popen(cmd).read() 3.commands 模块,其实也是对popen的封装. 此模块主要有如下方法:commands.getstatusoutput(cmd) 返回(status, output).commands.getoutput(cmd) 只返回输出结果comma