ZipFile.CreateFromDirectory throws 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
实际上我正在尝试创建一个目录的 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.
以下是它的代码片段。 :
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);
}
非常感谢您的帮助。提前致谢。 :)
你得到这个异常才有意义。让我们逐步研究您的代码:
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);
}
虽然,即使你删除文件,你也可能会遇到同样的错误。
问题是,当您尝试将文件夹 C:\Temp
压缩到文件 C:\Temp\myZipFile.zip
中时,您也会尝试压缩文件本身。这实际上是您获取文件正在使用错误的地方。
所以,
将Directory.Exists()替换为File.Exists()
压缩到另一个文件夹
只是一个友好的警告,如果我是你的话,我会对 Directory.Delete() 保持谨慎:)
正确代码:
这段代码经过少量修改后对我有用..
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);
我的问题是,输出文件夹和压缩文件夹相同。
移动到单独的文件夹,现在工作正常。
实际上我正在尝试创建一个目录的 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.
以下是它的代码片段。 :
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);
}
非常感谢您的帮助。提前致谢。 :)
你得到这个异常才有意义。让我们逐步研究您的代码:
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);
}
虽然,即使你删除文件,你也可能会遇到同样的错误。
问题是,当您尝试将文件夹 C:\Temp
压缩到文件 C:\Temp\myZipFile.zip
中时,您也会尝试压缩文件本身。这实际上是您获取文件正在使用错误的地方。
所以,
将Directory.Exists()替换为File.Exists()
压缩到另一个文件夹
只是一个友好的警告,如果我是你的话,我会对 Directory.Delete() 保持谨慎:)
正确代码:
这段代码经过少量修改后对我有用..
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);
我的问题是,输出文件夹和压缩文件夹相同。
移动到单独的文件夹,现在工作正常。