当前上下文中不存在名称为“zipfile”的内容。

16

我有一个可以正常运行的SSIS项目,但当我尝试编辑它时,出现以下错误:

当前上下文中不存在名称为'zipfile'的变量

不进行编辑时,项目可以正常工作。

导致错误的代码:

public void Main()
{
    // TODO: Add your code here
    string moduleName = Dts.Variables["User::ModuleName"].Value.ToString();
    string s = Dts.Variables["User::ZipFileLocation"].Value.ToString().TrimEnd('\\') + "\\" + moduleName + "\\" + moduleName + "_" + DateTime.Now.ToString("ddMMyyyy");

    // TODO: Add your code here
    string startPath = s;
    string zipPath = s + ".zip";

    try
    {
        File.Delete(zipPath);
        ZipFile.CreateFromDirectory(startPath, zipPath);
    }
    catch (Exception e)
    {
    }

    Dts.TaskResult = (int)ScriptResults.Success;
}

我该怎么解决这个问题?


5
您可能只参考了System.IO.Compression,但奇怪的是您还需要引用System.IO.Compression.FileSystem。 - Nils O
@NilsO 这就是我的问题所在。不确定为什么我们必须添加一个单独的引用,但无论如何,我已经在我的端口解决了这个问题。 - Chad
@Chad,这是因为Zipfile类在System.IO.Compression.FileSystem程序集中,而它在System.IO.Compression命名空间中。 - Nils O
4个回答

21

确保您正在使用.NET 4.5版本。引用压缩DLL - 这是路径:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll

通过添加using System.IO.Compression.FileSystem在类中引用它。如果该类从另一个类继承,请确保在父类中也引用它。(这是我编译代码所必须做的)


你不需要dlls,只需使用以下链接https://dev59.com/jl0a5IYBdhLWcg3w88rU#43196451 - Toolkit

6
为了使用ZipFile类,您必须在项目中添加对System.IO.Compression.FileSystem程序集的引用;否则,当尝试编译时,将会得到以下错误消息:
“当前上下文中不存在‘ZipFile’的名称。”
有关如何在Visual Studio中向项目添加引用的更多信息,请参见使用引用管理器添加或删除引用

3
我发现 ZipFile 类只使用 System.IO.Compression 无法合作,它要求看到对 System.IO.Compression.FileSystem引用
如果您使用 Visual Basic,则添加引用非常容易。 在解决方案资源管理器中,项目下的一个选项卡称为References。 在那里右键单击并选择Add Reference。 向下滚动一点并选中System.IO.Compression.FileSystem旁边的复选框。 一旦您点击OK,甚至不需要在代码中显式引用System.IO.Compression.FileSystem
祝您好运! (:

1

仅供更新:

使用 .Net 4.6.1 版本

添加对 System.IO.Compression.FileSystem 的引用,并使用 using System.IO.Compression 即可。

using System.IO.Compression.FileSystem 会导致以下错误。

Reference error


是的,我也看到了同样的东西。 - Jonathan Rys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接