使用ZipFile压缩:mscorlib.dll中发生未处理的“System.IO.IOException”类型异常

时间:2015-05-08 03:41:08

标签: c# compression zip zipfile

我不确定为什么在成功压缩文件夹内容后发生此异常。这应该是对的吗?

错误:mscorlib.dll中出现未处理的“System.IO.IOException”类型异常 附加信息:进程无法访问文件'c:\ Temp \ pack.zip',因为它正由另一个进程使用。

    private static string directoryPath = @"c:\Temp\";
    static void Main(string[] args)
    {
        zipFolder(directoryPath, directoryPath+@"pack.zip");
    }

    public static void zipFolder(string targetPath, string resultPath)
    {
        ZipFile.CreateFromDirectory(targetPath, resultPath,CompressionLevel.Optimal,true);
    }

1 个答案:

答案 0 :(得分:1)

您在代码中所做的是在尝试在相同目录中创建zip文件时阅读C:\ Temp的内容。

而是在app目录中创建一个文件,稍后将文件复制到Temp文件夹。

        var newFilePath = Path.Combine(directoryPath, "pack.zip");
        if(File.Exists(newFilePath))File.Delete(newFilePath); //Remove file if it exists
        if (File.Exists("pack.zip")) File.Delete("pack.zip"); //Remove file if it exists
        zipFolder(directoryPath, "pack.zip");
        File.Move("pack.zip", newFilePath);
相关问题