简体   繁体   English

DotNetZip提取阻止进程访问文件

[英]DotNetZip extract prevents process from accessing file

I'm using the DotNetZip library to extract files from a zip file. 我正在使用DotNetZip库从zip文件中提取文件。

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. 我得到一个例外: 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. File.ReadAllLines(upd)之前的唯一使用位置是在上面的DotNetZip代码中。

Is there a way in the DotNetZip code to prevent this from happening? DotNetZip代码中是否有一种方法可以防止这种情况发生?

The problem it's not from DotNetZip. 问题不在于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. 如果您使用Windows7或Windows8,则可以使用内置的资源监视器。 Read this post: How to know what process is using a given file? 阅读这篇文章: 如何知道正在使用给定文件的进程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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