C#.NET打开使用System.IO.Compression创建的ZipArchive时出现缺失方法异常.NET应用程序,我正在尝试写入zip存档并使用System.IO.Compression从中读取。现在我创建了ziarchive:publicvoidSaveStdV20ZipProject(stringstrfilepath,clsProjectInfoGameInfo){using(varms=newMemoryStream()){using(vararchive=newZipArchive(ms,ZipArchiveMode.Create,true)){stringstrProjectData=String。空的;StringBuildersb=newStringBuilder();//首先,我们添加游戏信息数据...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());某人追加线(GameInfo.intTotalCategories.ToString());sb.AppendLine(GameInfo.blTiebreakerPresent.ToString());//接下来,为游戏数据字符串创建一个存档条目...strProjectData=sb.ToString();varProjectData=archive.CreateEntry("ProjectData.txt");使用(varentryStream=ProjectData.Open())使用(varstreamWriter=newStreamWriter(entryStream)){streamWriter.Write(strProjectData);}//我们完成了为该项目编写所有数据。现在让我们将它写入文件...使用(varfileStream=newFileStream(@strfilepath,FileMode.Create)){ms.Seek(0,SeekOrigin.Begin);ms.CopyTo(文件流);}}}}以下是我如何打开它:publicvoidOpenStdV20ZipProject(stringstrfilepath){stringzipPath=strfilepath;字符串extractPath=Path.GetTempFileName();使用(ZipArchivearchive=ZipFile.OpenRead(zipPath)){foreach(ZipArchiveEntryentryinarchive.Entries){if(entry.FullName.EndsWith(".txt",StringComparison.OrdinalIgnoreCase)){使用(StreamReadersr=newStreamReader(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());}}}}}//<-这是第1320行,它抛出一个缺少方法的异常,我在网上查了很多资料这是堆垛跟踪:System.MissingMethodExceptionoccurredHResult=0x80131513Message=Methodnotfound:'System.IO.Compression.ZipArchiveSystem.IO.Compression.ZipFile.OpenRead(System.String)'。Source=TASv20ClsLibStackTrace:atTASv20ClsLib.clsOpenStandardProject.OpenStdV20ZipProject(Stringstrfilepath)inC:UsersReubenDocumentsVisualStudio2017ProjectsC#ProjectsTRIVIAAUTHORSUITEV20PROJECTS2TASv20ZipTestJun142TASv20ClsLibClass1.cs:line1320atTrivia_Author_v20.frmMain.openV20ProjectToolStripMenuItem_Click(Objectsender,EventArgse)在C:UsersReubenDocumentsVisualStudio2017ProjectsC#ProjectsTRIVIAAUTHORSUITEV20PROJECTS2TASv20ZipTestJun142TriviaAuthorv10NewApproachfrmMain.cs:System.Windows.Forms.ToolStripItem.RaiseEvent(Objectkey,EventArgse)处的第1627行。System.Windows.Forms.ToolStripItem.HandleClick(EventArgse)在System.Windows.Forms.ToolStripItem.HandleMo上的Forms.ToolStripMenuItem.OnClick(EventArgse)在System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgse,ToolStripItemEventType满足)在System.Windows.Forms.ToolStripItem.FireEvent(EventArgse,ToolStripItemEventType满足)MouseEventArgsmea)在System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgsmea)在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32clicks)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ScrollableControl.WndProc(Message&m)在System.Windows.Forms.ToolStrip.WndProc(Message&m)在System.Windows.Forms.ToolStripDropDown.WndProc(Message&m)在System.Windows.Forms。Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)在System.Windows.Windows的System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32reason,Int32pvLoopData)。System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)在Trivia_Author_v20的System.Windows.Forms.Application.Run(表单主窗体)处的Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)。Program.Main(String[]args)inC:UsersReubenDocumentsVisualStudio2017ProjectsC#ProjectsTRIVIAAUTHORSUITEV20PROJECTS2TASv20ZipTestJun142TriviaAuthorv10NewApproachProgram.cs:line126ZipFile.OpenRead(string)方法仅在.NET4.5中添加它在以前的版本中不存在。您的问题不清楚您的项目针对哪个版本的.NET,以及您尝试在哪个版本的.NET安装上运行它,但毫无疑问您的目标是.NET4.5或更高版本,但正在尝试运行只有较旧版本的.NET的代码。要解决此问题,请确保在要运行代码的计算机上安装了.NET4.5,或者使用较旧的API。例如,您可以轻松编写自己的OpenRead(string)方法:ZipArchiveOpenRead(stringfilename){returnnewZipArchive(File.OpenRead(filename),ZipArchiveMode.Read);在这里我使用了Ionic并且为了让它工作,你可以使用你需要导入到你的项目中的Ionic.zip。使用(varzip=Ionic.Zip.ZipFile.Read(“YourFilePAth”)){在此处输入代码};如果exception分享的所有内容对您有用,需要进一步了解C#学习教程,希望您多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
