"zipfile"这个名称在当前上下文中不存在

vig*_*esh 14 c# ssis zipfile

我有一个可以按原样运行的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;
}
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

小智 18

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


小智 5

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

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

有关如何在 Visual Studio 中添加对项目的引用的详细信息,请参阅如何:使用引用管理器添加或删除引用