I have to 2 process excel. For example:
1) example1.xlsx 2) example2.xlsx
How to kill first "example1.xlsx"?
I use this code:
foreach (Process clsProcess in Process.GetProcesses())
if (clsProcess.ProcessName.Equals("EXCEL")) //Process Excel?
clsProcess.Kill();
That kill a both. I wanna kill just one... Thank you.
just did a quick search on Google, try
Process.MainWindowTitle()
to get the title of the Excel process, and decide which one is that you want to kill.I am not sure about this method, but hope this will help:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowtitle.aspx
You need to check file handles, that are opened by process and then kill it.
How to check which file handles process is holding: How do I get the list of open file handles by process in C#?
Use below logic to prevent Zombie Excel processes in Task Manager
kd7's post is an awesome answer and works well, just two things to add,
MainWindowTitle
format is -"Filename.xlsx - Excel"
If your excel document is not visible then your
MainWindowTitle
will be""
using the""
forMainWindowTitle
will kill all zombie excel process'.Copy and paste this. Its done!
The ProcessMainWindow Title will do it for you, it appends "Microsoft Excel - " to the name of the file:
So essentially (quick code):
Use:
Edit: Tested and verified to work.