c-sharp

Close all excel process using C#

The code can be used to close all the excel processes running using C# code.

public async Task<bool> closeExcelProcesses()
{
    try
    {
        System.Diagnostics.Process[] process=System.Diagnostics.Process.GetProcessesByName("Excel");
        foreach (System.Diagnostics.Process p in process)
        {
            if (!string.IsNullOrEmpty(p.ProcessName))
            {
                p.Kill();
            }
        }
        TraceService("ALL EXCEL PROCESSES CLOSED");
        return true;
    }
    catch(Exception ex)
    {
        TraceService("ERROR IN CLOSING EXCEL PROCESSES");
        return false;
    }
}
Was this helpful?