安装了 .Net 4.5 但不能在 Visual C# 中使用 ZipFile 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14218602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:06:08  来源:igfitidea点击:

Installed .Net 4.5 but can't use ZipFile class in Visual C#

c#.netvisual-studio-2012compressionzipfile

提问by wuxilixi

I'm kind of a newbie to Visual Studio programming.

我是 Visual Studio 编程的新手。

I recently upgraded .Net 4.0 to 4.5 in order to use the ZipFile class under System.IO.Compression, but after the installation completed, Visual Studio (I'm using 2012) still cannot recognize ZipFile as a class name.

我最近将 .Net 4.0 升级到 4.5,以便使用 System.IO.Compression 下的 ZipFile 类,但是安装完成后,Visual Studio(我使用的是 2012)仍然无法将 ZipFile 识别为类名。

I've made sure that .Net 4.5 appears in Control Panel programs list and my C# solution sets .Net Framework 4 as the target framework.

我已确保 .Net 4.5 出现在控制面板程序列表中,并且我的 C# 解决方案将 .Net Framework 4 设置为目标框架。

Could someone help me figure this out?

有人可以帮我解决这个问题吗?

采纳答案by Daniel Kelley

See ZipFile Classon MSDN. It shows the required framework version is 4.5. Once the framework version is fixed check you have added a reference to the System.IO.Compression.FileSystem.dllassembly and added a using System.IO.Compressiondirective to your class.

请参阅MSDN 上的ZipFile 类。它显示所需的框架版本是 4.5。一旦框架版本被修复,请检查您是否添加了对System.IO.Compression.FileSystem.dll程序集的引用System.IO.Compression并向您的类添加了 using指令。

回答by daryal

You need to change the target framework of the current project from .Net 4 to .Net 4.5.

您需要将当前项目的目标框架从 .Net 4 更改为 .Net 4.5。

回答by platon

You also need to reference the System.IO.Compression.FileSystem.dll assembly.

您还需要引用 System.IO.Compression.FileSystem.dll 程序集。

回答by Kiquenet

New features in .NET 4.5

.NET 4.5 中的新功能

Zip compression improvements to reduce the size of a compressed file. See the System.IO.Compression namespace.

Zip 压缩改进以减少压缩文件的大小。请参阅 System.IO.Compression 命名空间。

Add System.IO.Compressionassembly as reference to your project. You may also want to reference System.IO.Compression.FileSystemassembly to access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.

添加System.IO.Compression程序集作为您项目的参考。您可能还想引用System.IO.Compression.FileSystem程序集来访问ZipArchive 类的三个扩展方法(来自 ZipFileExtensions 类):CreateEntryFromFile、CreateEntryFromFile 和 ExtractToDirectory。这些扩展方法使您能够将条目的内容压缩和解压缩到文件中。

Sample

样本

const string zipFilePath = @"C:\apps\Sample Pictures.zip";

using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read)) {

        foreach (var zipArchiveEntry in archive.Entries)
            Console.WriteLine(
                "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
            );
}

References:
http://msdn.microsoft.com/en-us/library/ms171868.aspx
http://www.tugberkugurlu.com/archive/net-4-5-to-support-zip-file-manipulation-out-of-the-box

参考资料:
http: //msdn.microsoft.com/en-us/library/ms171868.aspx
http://www.tugberkugurlu.com/archive/net-4-5-to-support-zip-file-manipulation-out开箱即用

回答by The Conspiracy

Just to further clarify the previous answers, here's how to add the references manually to a Web.config:

只是为了进一步澄清以前的答案,以下是如何将引用手动添加到 Web.config:

<configuration>
  <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <add assembly="System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

Or to a *.csproj:

或到 *.csproj:

<Project ...>
  <ItemGroup>
    <Reference Include="System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089, processorArchitecture=MSIL" />
    <Reference Include="System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089, processorArchitecture=MSIL" />
  </ItemGroup>
</Project>

The files can be found in C:\Windows\Microsoft.NET\assembly\GAC_MSIL\ and the subfolders contain the necessary info on version, culture and PublicKeyToken as well.

这些文件可以在 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\ 中找到,子文件夹还包含有关版本、文化和 PublicKeyToken 的必要信息。

回答by Andy Weinstein

For Windows Phone 8.1, use NuGet to add the Microsoft Compression package to your project and reference it.

对于 Windows Phone 8.1,使用 NuGet 将 Microsoft 压缩包添加到您的项目并引用它。

If you had an older WP8 project you may have been using a different package that would create conflicts with the System.IO.Compression dll that is part of the .NET 4.5 that comes with WP8.1. You need to get rid of that and use Microsoft Compression which works harmoniously with .NET 4.5. That's how I got here!

如果您有一个较旧的 WP8 项目,您可能一直在使用不同的包,这会与 System.IO.Compression dll 产生冲突,后者是 WP8.1 附带的 .NET 4.5 的一部分。您需要摆脱它并使用与 .NET 4.5 协调工作的 Microsoft Compression。我就是这样过来的!

回答by Hugo Nava Kopp

In my case I needed to manually delete all dll references that started with System.IO.Compressionand then add one by one the needed ones (System.IO.Compression.FileSystem, etc.) even though I only wrote a single using statement

就我而言,我需要手动删除所有以System.IO.Compression开头的 dll 引用,然后逐个添加所需的引用System.IO.Compression.FileSystem等),即使我只写了一个 using 语句

using System.IO.Compression;

I hope this helps someone

我希望这可以帮助别人