尝试从 ZipArchive C# 读取 ZipFile 时出现 System.MissingMethodException [重复]

问题描述 投票:0回答:3

我有一个 C# .NET (v4.6.2) WinForms 应用程序,我正在其中访问一个文件,该文件可能是也可能不是使用“System.IO. Compression;”创建的 .zip 存档。我在项目中都有“System.IO.Compression”和 System.IO.compress.FileSystem”引用,并且在顶部有“using System.IO.Compression;”,它是使用 NuGet 包安装程序安装的。

下面是尝试将文件作为 .zip 存档打开的代码:

      try
        {
            string extractPath = Path.GetTempFileName();
            string strGameVersion = "";
            string strProjectType = "";

            using (ZipArchive archive = ZipFile.OpenRead(OpenFilePath))
            {
                FileStream fs = new FileStream(extractPath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.FullName.Contains("ProjectData.txt"))
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
                        strGameVersion = sr.ReadLine();
                        strProjectType = sr.ReadLine();
                    }
                    File.Delete(extractPath);
                }
                sr.Close();
                fs.Close();
                archive.Dispose();
            }
    }
    catch(System.IO.FileFormatException flex1)
    {
        MessageBox.Show(flex1.ToString(), "oops.", MessageBoxButtons.OK,  MessageBox.Icon.Error);
    }

错误消息为“System.MissingMethodException:找不到方法:'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'”。 那么我做错了什么或者根本没有做什么?

c# .net winforms c#-ziparchive
3个回答
10
投票

由于某种原因,

OpenRead
程序集中不存在
net46
。 快速解决方法是使用

ZipArchive OpenRead(string filename)
{
    return new ZipArchive(File.OpenRead(filename), ZipArchiveMode.Read);
}

如回答https://stackoverflow.com/a/44598092/75947


1
投票

我必须切换到使用 nuget.org 中的 System.IO.Compression 才能使其正常工作。另外,我必须做出菲利克斯上面建议的更改。即替换:

ZipFile.OpenRead(file))

new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read)


0
投票

根据您的输入,我假设尝试加载的依赖项程序集可能是不正确的版本。为了告诉他们,您必须检查融合绑定日志以了解发生了什么。下面的教程讲述了如何调试程序集绑定失败以检测其根本原因。

http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx

© www.soinside.com 2019 - 2024. All rights reserved.