Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Deleting a dynamically created Excel file

Deleting a dynamically created Excel file

Scheduled Pinned Locked Moved C#
help
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    snir_ya
    wrote on last edited by
    #1

    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.

    J M 2 Replies Last reply
    0
    • S snir_ya

      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.

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      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 to Marshal.ReleaseComObject(aComObject); all those Excel objects being created. hope this helps . . .

      S 1 Reply Last reply
      0
      • J Jimmanuel

        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 to Marshal.ReleaseComObject(aComObject); all those Excel objects being created. hope this helps . . .

        S Offline
        S Offline
        snir_ya
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S snir_ya

          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.

          M Offline
          M Offline
          mnaveed
          wrote on last edited by
          #4

          // 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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups