sevenzipsharp file lock. I can't move a file?

949 views Asked by At

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?

2

There are 2 answers

0
pingsft On

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();
1
Peter Takacs On

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);
}