Kill Process Excel C#
The ProcessMainWindow Title will do it for you, it appends "Microsoft Excel - " to the name of the file:
So essentially (quick code):
private void KillSpecificExcelFileProcess(string excelFileName)
{
var processes = from p in Process.GetProcessesByName("EXCEL")
select p;
foreach (var process in processes)
{
if (process.MainWindowTitle == "Microsoft Excel - " + excelFileName)
process.Kill();
}
}
Use:
KillSpecificExcelFileProcess("example1.xlsx");
Edit: Tested and verified to work.
If your current code is working, this amendment should kill the first process it finds with the name "EXCEL".
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
If you want to kill a specific process, you're going to have to give a bit more information.
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 ""
for MainWindowTitle
will kill all zombie excel process'.