1、打开一个进程“Process1.exe”
1 //打开一个进程 2 private Process p; 3 public void StartPro() 4 { 5 Process[] temp = Process.GetProcessesByName("Processs1");//在所有已启动的进程中查找需要的进程; 6 if (temp.Length > 0)//如果查找到 7 { 8 return; 9 }10 else11 {12 p = new System.Diagnostics.Process(); System.IO.Directory.SetCurrentDirectory(@"E:\"); 13 p.StartInfo.FileName = @"E: \Processs1.exe";14 15 //p.StartInfo.Arguments = inpara + outpara; 16 //p.StartInfo.UseShellExecute = false;17 //p.StartInfo.RedirectStandardInput = true;18 //p.StartInfo.RedirectStandardOutput = true;19 //p.StartInfo.RedirectStandardError = true;20 //p.StartInfo.CreateNoWindow = true;21 //p.EnableRaisingEvents = true;22 23 p.Start();24 } 25 }
2、关闭一个进程“Process2.exe”
//关闭进程 public void StopPro() { Process[] processes = Process.GetProcessesByName("Process2");//在所有已启动的进程中查找需要的进程; foreach (Process p in processes) { //if (System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "IRAgent.exe") == p.MainModule.FileName) if (p.MainModule.FileName == @"E:\Process2.exe") { p.Kill(); p.Close(); } } }