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;//禁止快捷键

 

posted @ 2019-01-25 14:55  叫夏洛啊  阅读(2752)  评论(0编辑  收藏  举报