[英]“An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll” whenever I try using this method
[英]Using ZipFile compression: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
我不确定在成功压缩文件夹内容后为什么会发生此异常。 这应该对吗?
错误 :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);
}
您在代码中所做的是在尝试在同一目录中创建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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.