打开用System.IO.Compression创建的ZipArchive时,C#.NET缺少方法异常 [英] C# .NET Missing Method Exception when opening ZipArchive created with System.IO.Compression

查看:147
本文介绍了打开用System.IO.Compression创建的ZipArchive时,C#.NET缺少方法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a C# WinForms .NET app in which I'm trying to write to a zip archive and read from it using System.IO.Compression.

现在我创建ziparchive:

Here's now I create the 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

它引发了Missing Method Exception(遗漏的方法异常),我在互联网上找来找寻解决方法。这是堆栈跟踪:

It throws a Missing Method Exception and I've looked high and low in the Internet for a fix. Here's the stack trace:

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:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\TASv20ClsLib\Class1.cs:line 1320
   at Trivia_Author_v20.frmMain.openV20ProjectToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\Trivia Author v10 New Approach\frmMain.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:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\Trivia Author v10 New Approach\Program.cs:line 126


推荐答案

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

The ZipFile.OpenRead(string) method was added only in .NET 4.5. It does not exist in previous versions.

您的问题不清楚您的项目目标是哪个.NET版本,也没有在尝试的位置安装哪个.NET版本。要运行它,但是毫无疑问,您的目标是.NET 4.5或更高版本,但是正在尝试运行仅安装了.NET较旧版本的代码。

Your question is not clear about which version of .NET your project targets, nor which version of .NET is installed where you are trying to run it, but undoubtedly, you have targeted .NET 4.5 or higher, but are trying to run the code on which only an older version of .NET is installed.

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

To fix this, either make sure .NET 4.5 is installed on the machine where you want to run the code, or use the older API. For example, you can write your own OpenRead(string) method without much difficulty:

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

这篇关于打开用System.IO.Compression创建的ZipArchive时,C#.NET缺少方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆