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