Got the two dots bad, one dot good thing down, marshalling com objects, setting everything to null at the end but Excel still won't get out of the task manager list. Here's what I have (try/catch/finally removed):
excelApp = new Application();
excelWbs = excelApp.Workbooks;
excelWb = excelWbs.Open(path);
excelWs = (Worksheet) excelWb.Worksheets["Information"];
cell = excelWs.Cells[2, 1];
ver = (cell as Range).Text;
excelWb.Close(false);
excelWbs.Close();
excelApp.Quit();
if (cell != null) Marshal.ReleaseComObject(cell);
if (excelWs != null) Marshal.ReleaseComObject(excelWs);
if (excelWb != null) Marshal.ReleaseComObject(excelWb);
if (excelWbs != null) Marshal.ReleaseComObject(excelWbs);
if (excelApp != null) Marshal.ReleaseComObject(excelApp);
cell = null;
excelWs = null;
excelWb = null;
excelWbs = null;
excelApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
I've left it in the task manager hoping it would be collected at some point but no luck, just sits there until I kill it in task manager. I've also tried killing Excel but keep getting "Access Denied" when I do. I've tried a number of ways to kill the process.
Process[] excelProcs = Process.GetProcessesByName("EXCEL");
foreach (Process proc in excelProcs)
proc.Kill();
or
int hWnd = excelApp.Application.Hwnd;
uint processID;
GetWindowThreadProcessId((IntPtr)hWnd, out processID);
Process.GetProcessById((int)processID).Kill();
Process.GetProcessById((int)processID).WaitForExit();
Still get the access denied error. What am I missing?
EDIT: Per Hans Passant Post
sWork = clsExcel.GetVersion();
GC.Collect();
GC.WaitForPendingFinalizers();
...
public string GetVersion()
{
excelApp = new Application();
excelWbs = excelApp.Workbooks;
excelWb = excelWbs.Open(path);
excelWs = (Worksheet) excelWb.Worksheets["Information"];
cell = excelWs.Cells[2, 1];
ver = (cell as Range).Text;
excelWb.Close(false);
excelWbs.Close();
excelApp.Quit();
}