c# - DotNetZip 提取阻止进程访问文件

标签 c# .net file .net-4.0 dotnetzip

我正在使用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();
}

稍后,我尝试编辑我提取的文件之一。

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
}    

在线

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

我得到异常:The process cannot access the file <file name> because it is being used by another process.

我知道抛出此异常是因为该文件正在其他地方使用。之前唯一使用过的地方File.ReadAllLines(upd)位于上面的 DotNetZip 代码中。

DotNetZip 代码中有办法防止这种情况发生吗?

最佳答案

问题不是来自 DotNetZip。我在我的项目中尝试了代码,它可以工作文件:

[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
            }
        }

可能另一个进程正在使用您尝试读取的文件。如果您使用的是Windows7或Windows8,则可以使用内置资源监视器。阅读这篇文章:How to know what process is using a given file?

关于c# - DotNetZip 提取阻止进程访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239723/

相关文章:

c# - 如何在 C# 中使用 LINQ 进行两级排序

c# - 如何比较忽略大小写的字典值

.net - Azure 最快的通信协议(protocol)

c# - 使WinForms富文本框忽略 "CTRL + >"和 "CTRL + <"热键

c - 从 C 中的文件描述符中检索文件名

c - 尝试在 C 中读取迷宫文本文件时出现 malloc 错误

c# - 我可以在没有存储帐户的情况下使用带有 HTTP 触发器的 Azure Function 吗

c# - 在 Win10 UWP App 中获取屏幕分辨率

.net - 更新到 2.10.1 后 webApp 不起作用

c - 如何将重定向输出写入 C 中循环内的文件?