【问题标题】:The name 'zipfile' does not exist in the current context当前上下文中不存在名称“zipfile”
【发布时间】:2023-03-05 07:51:01
【问题描述】:

我有一个可以按原样运行的 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;
}

我该如何解决这个问题?

【问题讨论】:

  • 你可能只引用了 System.IO.Compression,奇怪的是你也需要引用 System.IO.Compression.FileSystem
  • @NilsO 这对我来说是个问题。不知道为什么我们必须为此添加单独的参考,但仍然解决了我的问题。
  • @Chad 这是因为 Zipfile 类位于 System.IO.Compression.FileSystem 程序集中,而它位于 System.IO.Compression 命名空间中。

标签: c# ssis zipfile


【解决方案1】:

确保您使用的是 .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 在类中引用它。如果该类是从另一个类继承的,请确保在父类中也引用它。 (这是我必须做的让它编译)

【讨论】:

【解决方案2】:

要使用ZipFile 类,您必须在项目中添加对System.IO.Compression.FileSystem 程序集的引用;否则,您在尝试编译时会收到以下错误消息:

当前上下文中不存在名称“ZipFile”。

有关如何在 Visual Studio 中添加对项目的引用的详细信息,请参阅How to: Add or remove references by using the Reference Manager

【讨论】:

    【解决方案3】:

    我发现ZipFile 类不能仅使用System.IO.Compression 进行协作,它要求查看System.IO.Compression.FileSystem引用

    如果您使用的是 Visual Basic,添加引用相当容易。在解决方案资源管理器中,项目下的选项卡之一称为 References。右键单击那里并选择添加参考。向下滚动一点,选中System.IO.Compression.FileSystem 旁边的复选框。单击确定后,您甚至不需要在代码中显式引用System.IO.Compression.FileSystem

    祝你好运(:

    【讨论】:

      【解决方案4】:

      仅供更新:-

      含.Net 4.6.1版本

      添加对System.IO.Compression.FileSystemusing System.IO.Compression 的引用就足够了。

      using System.IO.Compression.FileSystem 给出以下错误。

      【讨论】:

      • 是的,我看到了同样的事情。
      猜你喜欢
      • 1970-01-01
      • 2017-07-15
      • 2013-10-02
      • 2014-04-22
      • 2017-06-17
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多