尝试从ZipArchive读取ZipFile时出现System.MissingMethodException异常

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: Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'.”,那么我做错了什么或者根本没做呢?
3个回答

10

由于某些原因,OpenReadnet46 程序集中不存在。一个快速的解决方法是使用

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

如在https://stackoverflow.com/a/44598092/75947中回答的那样。


1
我不得不转而使用来自nuget.org的System.IO.Compression才能使它工作。此外,我还必须进行Felix上面建议的更改。也就是替换:

ZipFile.OpenRead(file))

使用new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read)进行读取。


0
根据您的输入,我推测尝试加载的依赖程序集可能是其不正确的版本。为了告诉它们,您需要检查融合绑定日志以查看发生了什么。下面的教程介绍了如何调试程序集绑定失败以检测其根本原因。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接