简体   繁体   English

ZipFile.CreateFromDirectory 抛出 System.IO.IOException:进程无法访问文件 X,因为它正被另一个进程使用

[英]ZipFile.CreateFromDirectory throws System.IO.IOException : The process cannot access the file X because it is being used by another process

Actually I'm trying to create zip file of a directory and but the ZipFile.CreateFromDirectory() giving below Exception.实际上,我正在尝试创建目录的 zip 文件,但是ZipFile.CreateFromDirectory()给出了以下异常。

System.IO.IOException : The process cannot access the file PATH_TO_CREATE_ZIP/file.zip' because it is being used by another process. System.IO.IOException:进程无法访问文件 PATH_TO_CREATE_ZIP/file.zip',因为它正被另一个进程使用。

Following is the Code Snippet for it.以下是它的代码片段。 :

public void createZipFile(string zipPath, string archiveFileName)
{
    string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;

    if (Directory.Exists(DirectoryToBeArchive + ".zip"))
    {
        File.Delete(DirectoryToBeArchive);
        ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
    }
    else
        ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);

    Directory.Delete(DirectoryToBeArchive);
}

Help Would be Much Appreciated.帮助将不胜感激。 Thanks in Advance.提前致谢。 :) :)

It only makes sense you get this exception. 您收到此异常才有意义。 Let's investigate your code step by step: 让我们逐步研究您的代码:

createZipFile("C:\\Temp", "myZipFile");

public void createZipFile(string zipPath, string archiveFileName)
{
    //DirectoryToBeArchive = "C:\\Temp\\myZipFile"
    string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;

    //Some logical error here, you probably meant to use File.Exists()
    //Basically, as you can't find a directory with name C:\\Temp\\myZipFile.zip, you always jump into else
    if (Directory.Exists(DirectoryToBeArchive + ".zip"))
    {
        File.Delete(DirectoryToBeArchive);
        ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
    }
    else
        //It will try to overwrite your existing "DirectoryToBeArchive".zip file 
        ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);

    //This won't work as well btw, as there probably is no directory 
    //with name C:\\Temp\\myZipFile
    Directory.Delete(DirectoryToBeArchive);
}

Though, even if you delete the file, you will probably hit same error. 但是,即使删除文件,也可能会遇到相同的错误。 The thing is when you try zipping the folder C:\\\\Temp into the file C:\\\\Temp\\\\myZipFile.zip you will also try zipping the file itself. 问题是,当您尝试将文件夹C:\\\\Temp压缩到文件C:\\\\Temp\\\\myZipFile.zip您还将尝试压缩文件本身。 That's actually where you get the file is being used error. 那实际上是您得到文件正在使用错误的地方。

So, 所以,

  1. Replace Directory.Exists() with File.Exists() 用File.Exists()替换Directory.Exists()

  2. Zip in another folder 在另一个文件夹中压缩

  3. Just a friendly warning, I'd be cautious with Directory.Delete() if I were you :) 只是一个友好的警告,如果我是你,我会对Directory.Delete()保持谨慎:)

Correct Code : 正确的代码:

this piece of code after little correction worked for me.. 经过一点点纠正的这段代码对我有用。

string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;

            if (File.Exists(DirectoryToBeArchive + ".zip"))
            {
                File.Delete(DirectoryToBeArchive + ".zip");
                ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
            }
            else
                ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);

            Directory.Delete(DirectoryToBeArchive , true);

My issue was, the output folder and zipping folder was the same.我的问题是,输出文件夹和压缩文件夹是一样的。
Moved to separate folders, now working fine.移动到单独的文件夹,现在工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# ZipFile.CreateFromDirectory - 进程无法访问文件“path_to_the_zip_file_created.zip”,因为它正被另一个进程使用 - c# ZipFile.CreateFromDirectory - the process cannot access the file "path_to_the_zip_file_created.zip" because it is being used by another process System.IO.IOException: '进程无法访问文件 '@.txt',因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.' 类型为“ System.IO.IOException”的未处理异常。该进程无法访问该文件,因为该文件正在被另一个进程使用 - An unhandled exception of type 'System.IO.IOException'.The process cannot access the file because it is being used by another process System.IO.IOException:进程无法访问文件“somefile.txt”,因为它正被另一个进程使用 - System.IO.IOException: The process cannot access the file 'somefile.txt' because it is being used by another process System.IO.IOException: '进程无法访问文件“文件位置”,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file "file location"because it is being used by another process.' System.IO.IOException:进程无法访问文件'.txt',因为它正由另一个进程使用 - System.IO.IOException: The process cannot access the file '.txt' because it is being used by another process 错误:帮助D:System.IO.IOException:该进程无法访问文件,因为它正在被另一个进程使用 - Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process System.IO.IOException: '进程无法访问该文件,因为它正被另一个进程使用 - System.IO.IOException: 'The process cannot access the file because it is being used by another process “ System.IO.IOException:该进程无法访问文件'C:\\ Test \\ test.txt',因为它正在被另一个进程使用” - “System.IO.IOException: The process cannot access the file 'C:\Test\test.txt' because it is being used by another process” 此代码给出此异常“System.IO.IOException:'该进程无法访问该文件,因为它正在被另一个进程使用。” ” - this code give this exception “System.IO.IOException: 'The process cannot access the file because it is being used by another process.' ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM