简体   繁体   中英

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

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM