man*_*mer 3 .net c# winforms ziparchive
我有一个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);
}
Run Code Online (Sandbox Code Playgroud)
错误消息是"System.MissingMethodException:Method not found:'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'." 那么我做错了什么或者根本没做什么?
Fel*_*lix 10
由于某种原因,OpenRead在net46汇编中不存在.快速的解决方法是使用
ZipArchive OpenRead(string filename)
{
return new ZipArchive(File.OpenRead(filename), ZipArchiveMode.Read);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3555 次 |
| 最近记录: |