日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/3/15 C# 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.exepsi.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) { //ProcessStartInfoProcessStartInfo 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);}}

轉載于:https://www.cnblogs.com/softidea/p/3199928.html

總結

以上是生活随笔為你收集整理的c#调用本地命令并截取Output的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。