The following code leaves a Microsoft Excel background process running, until after my program has exited:
var excelApplication = new Application();
var workbooks = excelApplication.Workbooks;
var workbook = excelApplication.Workbooks.Open(file.FullName);
workbook.Close();
excelApplication.Workbooks.Close();
excelApplication.Quit();
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excelApplication);
Why? What am I missing?
Got it!
application.Workbooks != application.Workbooks
This property doesn't expose a variable, it generates a value. So every time I access the Workbooks property I create a new COM object.
I fixed the code and all is well. Thanks, everybody.
var excelApplication = new Application();
var workbooks = excelApplication.Workbooks;
var workbook = workbooks.Open(pathToExcelWorkbook); // Fixed
workbook.Close();
workbooks.Close();
excelApplication.Quit();
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excelApplication);