2011年8月3日 星期三

Can't quit Excel process when the program writes something to a excel file

  1. Get all processes before the program writes to the excel file.
  2. Writes to the excel file.
  3. Get all processes and find new process id. Quit the new process by the process id. 

Process[] originalProcesses = Process.GetProcessesByName("Excel");

Application application = new ApplicationClass();

Process[] newProcesses = Process.GetProcessesByName("Excel");

ArrayList list = new ArrayList();
foreach (Process newProcess in newProcesses) {
bool isNew = true;
foreach (Process originalProcess in originalProcesses) {
if (originalProcess.Id == newProcess.Id) {
isNew = false;
break;
}
}
if (isNew) list.Add(newProcess);
}

// Do your stuff

foreach (Process process in list) {
process.Kill();
}

沒有留言: