views:

602

answers:

3

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!

+4  A: 

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

Gonzalo Quero
A: 

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
A: 

Microsoft says the following in this article.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

My suggestion is to use SpreadsheetGear for .NET which my company sells, or one of our competitors (just Google "SpreadsheetGear" and look at the AdWords to see who they are).

Joe Erickson
I have seen this before, and while I would like to have a different solution, I cannot purchase any software at this time.
XPHiLtER
Microsoft used to give away SpreadsheetGear 2006 as part of the Visual Studio Registration Benefits. I don't know whether they still do - but you might take a look if your project would allow you to use a free 3rd party component.
Joe Erickson