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



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

错误:MSCORLIB.DLL中发生了一个未经处理的'system.io.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);
    }

您在代码中所做的工作是在尝试在 same> same Directory中创建zip文件时读取c: temp的内容。

而是在应用程序目录中创建一个文件,然后将文件复制到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);

最新更新