Today, I was asked to kill the excel process which opened by other program.
Since I can not change that program, I have to do some improvement in my code.
At first, it is really difficult for me to search a good way to kill the process,
but I was fortunate to find an effective method to achieve it.
So, no more nonsense, following is the code:
//kill the Excel process which is opened by SAP automatically
foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
{
if (process.ProcessName.ToUpper().Equals("EXCEL") &&
process.MainWindowTitle.Equals("yourEXCELName.xlsx - Excel"))
{
process.Kill();
}
}
tips: if you can not confirm your Excel’s mainWindowTitle, just to use console to show you
It’s not very hard, but it’s quietly useful and effectively.
Sorry for my language skill, I’m preparing for my English test this weekend.
Anyway, if it helps you, can you give a like ! ! Thank you ! !