【问题标题】:System.MissingMethodException when trying to read ZipFile from ZipArchive C# [duplicate]尝试从 ZipArchive C# 读取 ZipFile 时出现 System.MissingMethodException [重复]
【发布时间】:2017-06-01 23:53:13
【问题描述】:

我有一个 C# .NET (v4.6.2) WinForms 应用程序,我正在访问一个可能/可能不是使用“System.IO.Compression;”创建的 .zip 存档的文件。我在项目中有“System.IO.Compression”和“System.IO.Compress.FileSystem”引用,顶部有“使用 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 ziparchive


    【解决方案1】:

    由于某种原因,OpenRead 不存在于 net46 程序集中。 快速的解决方法是使用

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

    https://stackoverflow.com/a/44598092/75947 回复

    【讨论】:

      【解决方案2】:

      我不得不切换到使用来自 nuget.org 的 System.IO.Compression 才能使其正常工作。另外,我必须做出 Felix 上面建议的更改。那就是替换:

      ZipFile.OpenRead(file))

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2018-06-27
          • 1970-01-01
          • 1970-01-01
          • 2021-07-24
          • 2018-12-10
          • 2013-04-25
          • 1970-01-01
          • 1970-01-01
          • 2013-11-19
          相关资源
          最近更新 更多