Kill Process Excel C#

后端 未结 10 1580
栀梦
栀梦 2020-12-09 12:45

I have to 2 process excel. For example:

1) example1.xlsx 2) example2.xlsx

How to kill first \"example1.xlsx\"?

I use this code:

   f         


        
10条回答
  •  甜味超标
    2020-12-09 13:01

    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.

提交回复
热议问题