This project has moved. For the latest updates, please go here.

Locked file

Nov 26, 2010 at 1:03 AM

I am having a problem with EPPlus not releasing it's lock on my file.  I am not sure how to force it to close, any suggestions?

var newFile = new FileInfo(@".\temp.xlsx");
var
 package = new ExcelPackage(newFile);
// Do some work

// Several attempts to force the file to close
package.Save();
package.Stream.Close();
package.Dispose();
string path = newFile.FullName;
newFile = null;
package = null;
var
 fileToDelete = new FileInfo(path);
fileToDelete.Delete(); // Cannot access due to another process locking the file



Coordinator
Nov 28, 2010 at 7:07 PM

I tried your code and the delete works fine.

Are you sure you don't have it open in Excel or from some where else?

If not, supply a full sample that fails so I can reproduce the error.

Jan

Jan 25, 2011 at 4:07 PM

I'm experiencing this issue myself. I've tried many ways of trying to kill off the object and failed. I'm using V2.8.0.1, .Net 4 C# with no other libraries other than the .Net ones.

I first open my "InquirySheet.xlsm", change the values in certain cells and then fire a SaveAs using a different filename.

This all works perfectly until, I decided to run the process again with out closing the application down despite using this: "using (ExcelPackage pck = new ExcelPackage(FInfo))" wrapped around my code to change the values then finally save as. When I re run this I get "The process cannot access the file 'C:\Dev\MarelConfigurator\Configurator\Configurator\ProductFiles\InquirySheet.xlsm' because it is being used by another process."

The only way I can reopen this file is to close the application fully and then re open it. It's nothing to do with the classic Visual Studio keeping locks on files either, I've checked this as a deployed package on a clean Windows XP SP3 machine and the same exception occurs.

Please can someone point me in the right direction? Otherwise it's back to the old Office PIA's and they're nasty, really nasty. Other than that everything else is great and works as it says on the tin!

Kind Regards, Dan R

Jan 26, 2011 at 2:28 PM

Ok, I tried all sorts on this but managed to fix it in the end.

Basically I created another thread from my form and passed it my template parameters. Whilst this was running I just looped until the thread terminates of it's own accord (ie finished) and that works perfectly. The file is no longer locked!!! :)

I would like to add that this library is absolutely brilliant and has meant my app is 1.5Mb in size, no crappy PIA's or Com objects etc!!!

Many Thanks,

Dan R