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); } }