C#判斷程序調用外部的exe已結束 和 調用cmd.exe執行命令並獲取返回結果


技術點一)
來源: C#如何判斷程序調用的exe已結束

方法一:這種方法會阻塞當前進程,直到運行的外部程序退出 System.Diagnostics.Process exep = System.Diagnostics.Process.Start(@"C:\Windows\Notepad.exe"); exep.WaitForExit();//關鍵,等待外部程序退出后才能往下執行 MessageBox.Show("Notepad.exe運行完畢"); 方法二:為外部進程添加一個事件監視器,當退出后,獲取通知,這種方法時不會阻塞當前進程,你可以處理其它事情 System.Diagnostics.Process exep = new System.Diagnostics.Process(); exep.StartInfo.FileName = @"C:\Windows\Notepad.exe"; exep.EnableRaisingEvents = true; exep.Exited += new EventHandler(exep_Exited); exep.Start(); //exep_Exited事件處理代碼,這里外部程序退出后激活,可以執行你要的操作 void exep_Exited(object sender, EventArgs e) {   MessageBox.Show("Notepad.exe運行完畢"); }

=========================================================================================================

技術點二)
//調用cmd.exe執行命令並獲取返回結果 受教於C#程序調用cmd執行命令
      RunCmd("ipconfig")

     //運行cmd命令 並獲取返回結果
string RunCmd(string cmd) { try { Process pro = new Process(); pro.StartInfo.FileName = @"cmd.exe"; pro.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動 pro.StartInfo.RedirectStandardInput = true; //接受來自調用程序的輸入信息 pro.StartInfo.RedirectStandardOutput = true; //由調用程序獲取輸出信息 pro.StartInfo.RedirectStandardError = true; //重定向標准錯誤輸出 pro.StartInfo.CreateNoWindow = true; //不顯示程序窗口 pro.Start(); pro.StandardInput.WriteLine($"{cmd}&exit"); pro.StandardInput.AutoFlush = true; string result = pro.StandardOutput.ReadToEnd(); richTextBox1.AppendText(Environment.NewLine); richTextBox1.AppendText(result); richTextBox1.AppendText("".PadLeft(30, '=')); pro.WaitForExit(); pro.Close(); return result; } catch (Exception ex) { Console.WriteLine(ex.Message); return string.Empty; } }

以下是輸出結果:

 

=========================================================================================================

技術點三)
// C# 使用cmd.exe調用adb.exe 直接進入shell內

int devicesCount = 0; // 自行獲取
string devicesName = "";// 自行獲取
if (devicesCount > 0)
{
  Process p = new Process();
  p.StartInfo.FileName = "cmd"; //設定程序名  
  p.StartInfo.Arguments = $"/K adb -s {devicesName} shell"; //設定程式執行參數  
  Console.WriteLine(p.StartInfo.Arguments);
  p.Start();
}

 

技術點四 )

C# winform webbrowser 瀏覽器控件禁用右鍵, 快捷鍵

  WebBrowser webBrowser1= new WebBrowser();
  webBrowser1.Url = new Uri(@"https://www.cnblogs.com/Katakana/");
  webBrowser1.IsWebBrowserContextMenuEnabled = false; //禁止右鍵
  webBrowser1.WebBrowserShortcutsEnabled = false;//禁止快捷鍵

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM