2
votes

I'm using the DotNetZip library to extract files from a zip file.

using(ZipFile zip = ZipFile.Read(zipLocation))
{
    foreach (ZipEntry entry in zip){
        entry.Extract(_updateDir);
        Log.Write("Unpacked: " + entry.FileName, Log.LogType.Info);
    }

    zip.Dispose();
}

Later on, I attempt to edit one of the files that I extracted.

var updateList = allFiles.Where(x => x.Contains(".UPD"));
foreach (string upd in updateList){
    string[] result = File.ReadAllLines(upd);
    int index = Array.IndexOf(result, "[Info]");

    //then I do stuff with index
}    

At the line

string[] result = File.ReadAllLines(upd);

I get the exception: The process cannot access the file <file name> because it is being used by another process.

I know that this exception is being thrown because the file is in use elsewhere. The only place it is in use before File.ReadAllLines(upd) is in the DotNetZip code above.

Is there a way in the DotNetZip code to prevent this from happening?

1

1 Answers

3
votes

The problem it's not from DotNetZip. I tried the code in my project and it works file:

[Test]
        public void Test2()
        {
            using (ZipFile zip = ZipFile.Read("D:/ArchiveTest.zip"))
            {
                foreach (ZipEntry entry in zip)
                {
                    entry.Extract("D:/ArchiveTest");
                }

                zip.Dispose();
            }

            var updateList = Directory.GetFiles("D:/ArchiveTest").Where(x => x.Contains(".UPD"));
            foreach (string upd in updateList)
            {
                string[] result = File.ReadAllLines(upd);
                int index = Array.IndexOf(result, "[Info]");

                //then I do stuff with index
            }
        }

Probably another process is using the file you are trying to read. If you have Windows7 or Windows8, you can use the built-in Resource Monitor. Read this post: How to know what process is using a given file?