Home » excel » .net – How to get the process ID by Excel object in C#

.net – How to get the process ID by Excel object in C#

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an object of Excel, sheet.Excel.Application eApp.

How do I get the process ID of this Excel application and how do I kill that process?

How to&Answers:
var app = new ApplicationClass();

...

app.Quit();

See MSDN

Answer:

In addition to the normal Quit method of the ApplicationClass class, you can try the below, though it is subject to some, but not all, of the pitfalls mentioned in the link @Mark Pim suggested.

System.Diagnostics.Process[] myProcesses;
// Returns array containing all instances of Excel.
myProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
    if (myProcess.MainWindowTitle == Globals.ThisWorkbook.Application.Caption)
    {
        myProcess.Kill();
    }
}