1

I am running sevenzipsharp on various archives and if it passes my test I'd like to move the archive into another folder. However I get an exception saying the file is in use by a process. I can't move it either in windows explorer however when i kill my app process I can move it. I suspect sevenzipsharp has a lock on the file so I can't move it.

I write using (var extractor= new SevenZipExtractor(fn)) {. I tried moving the file outside the using block and still no go. It seems that after I run this method a few times I can move the first archive but than I won't be able to move the last archive

How do I make it so no process is using the file so I can move the archive to a folder?

nbanic
  • 1,270
  • 1
  • 8
  • 11
  • That is what's nice about open source, you can fix the bugs yourself. Use the project's Issue Tracker if you want the author to do it. I suspect he'll want a repro project, a rather reasonable expectation. – Hans Passant Jan 11 '12 at 20:57

2 Answers2

1

I could make this work (even with invalid archives) with the following code:

try 
{
    extractor.ExtractArchive(tempFolder);
}
finally
{
    extractor.Dispose();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    Directory.Delete(tempFolder, true);
}
  • I'm not crazy about this solution because isn't GC.Collect() an expensive method because it works on the entire .NET runtime? However, it is an effective workaround. There is an issue documented for this at https://archive.codeplex.com/?p=sevenzipsharp (see "Not disposing properly, leaving file in use.") The author said he would fix it back in 2009, and the item, #3743, was closed in 2013 but I grabbed the distro this year and it still has the problem. I'm using SevenZipSharp.dll v0.64.3890.29348, obtained from NuGet. – Craig Silver May 17 '18 at 16:06
0

Just dispose() this object and don't use using(.... ) I don't know why(!) but this method worked for me.

But if archive is not valid, it will be remain locked... Any suggestion?

UPDATE: There is a better way. You can make a Stream for your file and close() it at end of your code.

Stream reader=new FileStream(filename, FileMode.Open);//You can change Open mode to OpenOrCreate
sevenZipExtractor extactor= new SevenZipExtractor((Stream)reader);

//Your code here.....

reader.Close();
extactor.Dispose();
nbanic
  • 1,270
  • 1
  • 8
  • 11
pingsft
  • 316
  • 3
  • 4