1

I have to run Excel on a server and to kill the excel.exe process I am using taskkill.exe:

System.Diagnostics.Process.Start("taskkill.exe", "-im excel.exe /f");

My only problem is that if there is someone who is also using the server who is working on excel (It could happen) then their processes will be killed also. Is there anyway around this to only kill the excel files created by my code? Any help would be great. Thanks!

  • Why are you needing to kill the excel process this way in the first place? – GEOCHET Jan 27 '09 at 18:39
  • None of the other ways that I should be able to kill the process work. For instance workbook.close(), app.quit(), and there are few others I tried... –  Jan 27 '09 at 18:41
  • XPHilter: You should be exploring why that doesn't work instead IMO. – GEOCHET Jan 27 '09 at 18:41
  • I have tried, but I'm not sure anyone knows, I got my solution from this earlier today: http://stackoverflow.com/questions/51462/killing-excelexe-on-server#312513 –  Jan 27 '09 at 18:43
  • V.nice approach. :-) You might also want to address the fundamental issues that could be causing Excel to hang in the first place. I laid out what I think is a very clean strategy here: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c/159419#159419 – Mike Rosenblum Feb 07 '09 at 18:55

2 Answers2

4

Keep track of the PID of the processes you launch and kill them with taskkill.exe /pid NUMBER

Gonzalo Quero
  • 3,303
  • 18
  • 23
0

If your able to get the PID using code (which I think you can) you can use the pid switch rather then the /im switch.

masfenix
  • 7,736
  • 11
  • 45
  • 60