【问题标题】:Using ZipFile compression: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll使用 ZipFile 压缩:在 mscorlib.dll 中发生“System.IO.IOException”类型的未处理异常
【发布时间】:2015-07-18 20:53:56
【问题描述】:

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

错误:在 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);
    }

【问题讨论】:

  • 您尝试压缩的文件之一正在被另一个进程访问。
  • 不相关,但你应该看看Path.Combine,而不是进行字符串连接来组装一个有效的路径。

标签: c# compression zip zipfile


【解决方案1】:

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

改为在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多