简体   繁体   English

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

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

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? 有人可以帮我解决这个问题吗?

See ZipFile Class on MSDN. 请参阅MSDN上的ZipFile类 It shows the required framework version is 4.5. 它显示所需的框架版本是4.5。 Once the framework version is fixed check you have added a reference to the System.IO.Compression.FileSystem.dll assembly and added a using System.IO.Compression directive to your class. 修复框架版本后,检查是否已添加对System.IO.Compression.FileSystem.dll程序集的引用,并向您的类添加了一个使用System.IO.Compression指令。

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

Just to further clarify the previous answers, here's how to add the references manually: 为了进一步阐明以前的答案,以下是手动添加引用的方法:

<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>

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的必要信息。

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

New features in .NET 4.5 .NET 4.5中的新功能

Zip compression improvements to reduce the size of a compressed file. Zip压缩改进以减小压缩文件的大小。 See the System.IO.Compression namespace. 请参见System.IO.Compression命名空间。

Add System.IO.Compression assembly as reference to your project. 添加System.IO.Compression程序集作为项目的参考。 You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from the ZipFileExtensions class) for the ZipArchive class : CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. 您可能还想引用System.IO.Compression.FileSystem程序集来访问ZipArchive类的三个扩展方法(来自ZipFileExtensions类):CreateEntryFromFile,CreateEntryFromFile和ExtractToDirectory。 These extension methods enable you to compress and decompress the contents of the entry to a file. 这些扩展方法使您能够将条目的内容压缩和解压缩到文件。

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://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://www.tugberkugurlu.com/archive/net-4-5-to-support-zip-file-manipulation-out-of-the-box

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 Compression包添加到项目中并引用它。

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. 如果你有一个较旧的WP8项目,你可能一直在使用一个不同的软件包,它会与作为WP8.1附带的.NET 4.5的一部分的System.IO.Compression dll产生冲突。 You need to get rid of that and use Microsoft Compression which works harmoniously with .NET 4.5. 您需要摆脱它并使用与.NET 4.5协调工作的Microsoft Compression。 That's how I got here! 我就是这样来的!

In my case I needed to manually delete all dll references that started with System.IO.Compression and 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 我希望这可以帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM