public void KillProcess(string aProcessName) { try { PerformanceCounter PC = new PerformanceCounter();//性能计数器 System.Diagnostics.Process[] ExcelProcesses; ExcelProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL"); foreach (System.Diagnostics.Process IsProcedding in ExcelProcesses) { if (IsProcedding.ProcessName.ToUpper() == "EXCEL") { PC.InstanceName = IsProcedding.ProcessName; DateTime start = IsProcedding.StartTime; TimeSpan ss = DateTime.Now - start; try { IsProcedding.Kill(); } catch (Exception ex) { } } } } catch (Exception ex) { } }
public void KillProcess(string aProcessName) { //Process myproc = new Process(); ////得到所有打开的进程 //try //{ // foreach (Process thisproc in Process.GetProcessesByName(aProcessName)) // { // if (!thisproc.CloseMainWindow()) // { // thisproc.Kill(); // } // } //} //catch (Exception Exc) //{ //} try { PerformanceCounter PC = new PerformanceCounter();//性能计数器 System.Diagnostics.Process[] ExcelProcesses; ExcelProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL"); foreach (System.Diagnostics.Process IsProcedding in ExcelProcesses) { if (IsProcedding.ProcessName == "EXCEL") { PC.InstanceName = IsProcedding.ProcessName; DateTime start = IsProcedding.StartTime; TimeSpan ss = DateTime.Now - start; if ((ss.Hours >= 1 || ss.Minutes > 10) || (startExcelTime.Day == start.Day && startExcelTime.Hour == start.Hour && startExcelTime.Minute == start.Minute && startExcelTime.Second == start.Second && startExcelTime.Millisecond == start.Millisecond)) { if (!IsProcedding.HasExited) { try { IsProcedding.Kill(); } catch (Exception ex) { } } } } } } catch (Exception ex) { } }
public void KillAllProcess() { while (Process.GetProcessesByName("EXCEL").Count(process => process.ProcessName == "EXCEL") > 0) { foreach (Process IsProcedding in Process.GetProcessesByName("EXCEL")) { if (IsProcedding.ProcessName == "EXCEL") { if (!IsProcedding.HasExited) { IsProcedding.Kill(); } } } } }
/// <summary> /// 强制结束Word进程(垃圾回收) /// </summary> public void KillWordProcess() { int ProceedingCount = 0; try { System.Diagnostics.Process[] ProceddingCon = System.Diagnostics.Process.GetProcesses(); foreach (System.Diagnostics.Process IsProcedding in ProceddingCon) { if (IsProcedding.ProcessName.ToUpper() == "WINWORD") { ProceedingCount += 1; IsProcedding.Kill(); } } } catch {} }