C# 尝试从ZipArchive C读取ZipFile时System.MissingMethodException#
我有一个C#.NET(v4.6.2)WinForms应用程序,我正在访问一个文件,该文件可能是/可能不是使用“System.IO.Compression;”创建的.zip存档文件。我在项目中有“System.IO.Compression”和“System.IO.Compression.FileSystem”引用,以及“using System.IO.Compression”;“在顶部,它是使用NuGet软件包安装程序安装的 以下是尝试以.zip存档打开文件的代码:C# 尝试从ZipArchive C读取ZipFile时System.MissingMethodException#,c#,.net,winforms,ziparchive,C#,.net,Winforms,Ziparchive,我有一个C#.NET(v4.6.2)WinForms应用程序,我正在访问一个文件,该文件可能是/可能不是使用“System.IO.Compression;”创建的.zip存档文件。我在项目中有“System.IO.Compression”和“System.IO.Compression.FileSystem”引用,以及“using System.IO.Compression”;“在顶部,它是使用NuGet软件包安装程序安装的 以下是尝试以.zip存档打开文件的代码: try
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)'”
那么,我做错了什么或根本没有做什么呢?根据您的输入,我假设尝试加载的依赖程序集可能是不正确的版本。为了告诉它们,您必须检查fusion绑定日志以查看发生了什么。下面的教程介绍了如何调试程序集绑定失败以检测其根本原因
由于某种原因,
OpenRead
在net46
程序集中不存在。
快速解决方法是使用
ZipArchive OpenRead(string filename)
{
return new ZipArchive(File.OpenRead(filename), ZipArchiveMode.Read);
}
正如在上回答的那样,我必须从nuget.org切换到使用System.IO.Compression来实现这一点。此外,我还必须进行Felix建议的上述更改。这就是替换:
ZipFile.OpenRead(文件))
与
new ZipArchive(File.OpenRead(File),ZipArchiveMode.Read)