Csharp/C#教程:打开使用System.IO.Compression创建的ZipArchive时出现C#.NET缺失方法exception分享


打开使用System.IO.Compression创建的ZipArchive时出现C#.NET缺失方法exception

我有一个C#WinForms .NET应用程序,我正在尝试写入zip存档并使用System.IO.Compression从中读取。

现在我创建了ziparchive:

public void SaveStdV20ZipProject(string strfilepath, clsProjectInfo GameInfo) { using (var ms = new MemoryStream()) { using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true)) { string strProjectData = String.Empty; StringBuilder sb = new StringBuilder(); // First, we add the Game Info data... sb.AppendLine(GameInfo.strGameVersion); sb.AppendLine(GameInfo.strProjectType); sb.AppendLine(GameInfo.strGameTitle); sb.AppendLine(GameInfo.strAuthor); sb.AppendLine(GameInfo.strCreationDate); sb.AppendLine(GameInfo.blTSImagePresent.ToString()); sb.AppendLine(GameInfo.blTSAudioPresent.ToString()); sb.AppendLine(GameInfo.blTSVideoPresent.ToString()); sb.AppendLine(GameInfo.blFSSImagePresent.ToString()); sb.AppendLine(GameInfo.blFSSAudioPresent.ToString()); sb.AppendLine(GameInfo.blFSSVideoPresent.ToString()); sb.AppendLine(GameInfo.intTotalQuestions.ToString()); sb.AppendLine(GameInfo.intTotalMediaItems.ToString()); sb.AppendLine(GameInfo.intTotalCategories.ToString()); sb.AppendLine(GameInfo.blTiebreakerPresent.ToString()); // Next, create an archive entry for the Game Data string... strProjectData = sb.ToString(); var ProjectData = archive.CreateEntry("ProjectData.txt"); using (var entryStream = ProjectData.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write(strProjectData); } // We're done writing all the data for this project. Now let's write it to the file... using (var fileStream = new FileStream(@strfilepath, FileMode.Create)) { ms.Seek(0, SeekOrigin.Begin); ms.CopyTo(fileStream); } } } } 

以下是我如何打开它:

  public void OpenStdV20ZipProject(string strfilepath) { string zipPath = strfilepath; string extractPath = Path.GetTempFileName(); using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { using (StreamReader sr = new StreamReader(extractPath)) { clsProjInfo.strGameVersion = (string)sr.ReadLine(); clsProjInfo.strProjectType = (string)sr.ReadLine(); clsProjInfo.strGameTitle = (string)sr.ReadLine(); clsProjInfo.strAuthor = (string)sr.ReadLine(); clsProjInfo.strCreationDate = (string)sr.ReadLine(); clsProjInfo.blTSImagePresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.blTSAudioPresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.blTSVideoPresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.blFSSImagePresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.blFSSAudioPresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.blFSSVideoPresent = Convert.ToBoolean(sr.ReadLine()); clsProjInfo.intTotalQuestions = Convert.ToInt32(sr.ReadLine()); clsProjInfo.intTotalMediaItems = Convert.ToInt32(sr.ReadLine()); clsProjInfo.intTotalCategories = Convert.ToInt32(sr.ReadLine()); clsProjInfo.blTiebreakerPresent = Convert.ToBoolean(sr.ReadLine()); } } } } } // <-THIS IS LINE 1320 

它抛出了一个缺失的方法例外,我在互联网上看起来高低不一。 这是堆栈跟踪:

 System.MissingMethodException occurred HResult=0x80131513 Message=Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'. Source=TASv20ClsLib StackTrace: at TASv20ClsLib.clsOpenStandardProject.OpenStdV20ZipProject(String strfilepath) in C:UsersReubenDocumentsVisual Studio 2017ProjectsC# ProjectsTRIVIA AUTHOR SUITE V20 PROJECTS 2TAS v20 Zip Test Jun14 2TASv20ClsLibClass1.cs:line 1320 at Trivia_Author_v20.frmMain.openV20ProjectToolStripMenuItem_Click(Object sender, EventArgs e) in C:UsersReubenDocumentsVisual Studio 2017ProjectsC# ProjectsTRIVIA AUTHOR SUITE V20 PROJECTS 2TAS v20 Zip Test Jun14 2Trivia Author v10 New ApproachfrmMain.cs:line 1627 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Trivia_Author_v20.Program.Main(String[] args) in C:UsersReubenDocumentsVisual Studio 2017ProjectsC# ProjectsTRIVIA AUTHOR SUITE V20 PROJECTS 2TAS v20 Zip Test Jun14 2Trivia Author v10 New ApproachProgram.cs:line 126 

ZipFile.OpenRead(string)方法仅在.NET 4.5中添加。 它在以前的版本中不存在。

您的问题不清楚您的项目所针对的.NET版本,以及您尝试运行它的.NET安装版本,但毫无疑问,您已针对.NET 4.5或更高版本,但正在尝试运行代码只安装了旧版本的.NET。

要解决此问题,请确保在要运行代码的计算机上安装了.NET 4.5,或使用较旧的API。 例如,您可以毫不费力地编写自己的OpenRead(string)方法:

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

在这里我使用了Ionic和它的工作,你可以使用

您需要将Ionic.zip导入到您的项目中。

使用(var zip = Ionic.Zip.ZipFile.Read(“YourFilePAth”)){在此处输入代码};

上述就是C#学习教程:打开使用System.IO.Compression创建的ZipArchive时出现C#.NET缺失方法exception分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1028178.html

(0)
上一篇 2022年1月10日
下一篇 2022年1月10日

精彩推荐