Fehu
Fehu

Reputation: 401

Can't use ZipArchive class

I'm developing on a machine running windows server 2012 r2, that by default must use the 4.5 framework version, but when I try to use the ZipArchive class, that is available only from the 4.5 .net framework, if I run this simple code:

<%@ Page Language="C#" ResponseEncoding="utf-8" Trace="true" %>
<%@ Import Namespace="System.IO.Compression" %>
<%
  ... variables etc ...

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{

}
%>

I get this error:

CS0246: The type or namespace name 'ZipArchive' could not be found (are you missing a using directive or an assembly reference?)

From the trace I got this about the framework:

Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248

I tried changing the framework clr version in iis manager, but the only options are 2.0 and 4.0. What I'm doing wrong? :/

Upvotes: 3

Views: 1877

Answers (1)

Joe Enos
Joe Enos

Reputation: 40413

Looks like the ASPX page can only reference 4.0 DLLs, not 4.5 ones, and the new ZipFile and ZipArchive stuff is only in the 4.5 framework and specific to 4.5. Not sure the details behind that, but that's just how it seems to behave.

To get around this and use these classes in your ASPX files, add the following do your system.web/compilation section in web.config:

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

Upvotes: 1

Related Questions