Deleting a dynamically created Excel file
-
Hi all, I've seen a lot of posts over the web about this issue but not even one(!) prudent solution. I'm creating an excel file. Filling in some data, saving the excel and then when i'm trying to delete it i get the following exception: "The process cannot access the file 'c:\temporaryFiles\ANLsnir_yarom_276200717848.xls' because it is being used by another process. " Here's a code extract of the problem, a lot of progarammers would appriciate an answer for this riddle: Excel.Application excelApp = new new Excel.Application(); Excel._Workbook workbook = (Excel._Workbook)excelApp.Workbooks.Add(Type.Missing); Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet; //write into the excel and... workbook.Close(ANL_Excel.vk_true, filePath, ANL_Excel.vk_false); excelApp.Save(filePath); excelApp.Quit(); Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); pProcess[0].Kill(); File.Delete(filePath); Many Thanks, Snir Yarom.
-
Hi all, I've seen a lot of posts over the web about this issue but not even one(!) prudent solution. I'm creating an excel file. Filling in some data, saving the excel and then when i'm trying to delete it i get the following exception: "The process cannot access the file 'c:\temporaryFiles\ANLsnir_yarom_276200717848.xls' because it is being used by another process. " Here's a code extract of the problem, a lot of progarammers would appriciate an answer for this riddle: Excel.Application excelApp = new new Excel.Application(); Excel._Workbook workbook = (Excel._Workbook)excelApp.Workbooks.Add(Type.Missing); Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet; //write into the excel and... workbook.Close(ANL_Excel.vk_true, filePath, ANL_Excel.vk_false); excelApp.Save(filePath); excelApp.Quit(); Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); pProcess[0].Kill(); File.Delete(filePath); Many Thanks, Snir Yarom.
two observations about your code snippet: 1. only the first process returned by
GetProcessesByName
is being killed; what if there is more than one EXCEL.exe? 2. none of the Excel COM objects are being released. Don't forget toMarshal.ReleaseComObject(aComObject);
all those Excel objects being created. hope this helps . . . -
two observations about your code snippet: 1. only the first process returned by
GetProcessesByName
is being killed; what if there is more than one EXCEL.exe? 2. none of the Excel COM objects are being released. Don't forget toMarshal.ReleaseComObject(aComObject);
all those Excel objects being created. hope this helps . . .Thanks for the reply. I've implmeneted your observations:
Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); foreach(Process prcs in pProcess) { if (prcs != null) prcs.Kill(); } //pProcess[0].Kill(); Marshal.ReleaseComObject(excelApp);
But still, i get the same exception when i'm trying to delete this file. Any suggestions? Thanks. -
Hi all, I've seen a lot of posts over the web about this issue but not even one(!) prudent solution. I'm creating an excel file. Filling in some data, saving the excel and then when i'm trying to delete it i get the following exception: "The process cannot access the file 'c:\temporaryFiles\ANLsnir_yarom_276200717848.xls' because it is being used by another process. " Here's a code extract of the problem, a lot of progarammers would appriciate an answer for this riddle: Excel.Application excelApp = new new Excel.Application(); Excel._Workbook workbook = (Excel._Workbook)excelApp.Workbooks.Add(Type.Missing); Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet; //write into the excel and... workbook.Close(ANL_Excel.vk_true, filePath, ANL_Excel.vk_false); excelApp.Save(filePath); excelApp.Quit(); Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); pProcess[0].Kill(); File.Delete(filePath); Many Thanks, Snir Yarom.
// Need this to clean up all refs workbook.Close(null,null,null); excelApp.Workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject (excelApp); System.Runtime.InteropServices.Marshal.ReleaseComObject (worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook); worksheet=null; workbook=null; excelApp= null; GC.Collect(); // final cleanup! then delete your file
Nav.