c#调用本地命令并截取Output
生活随笔
收集整理的這篇文章主要介紹了
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:
轉載于:https://www.cnblogs.com/softidea/p/3199928.html
總結
以上是生活随笔為你收集整理的c#调用本地命令并截取Output的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux性能监控-Top
- 下一篇: poj 3685