用于重命名的 C# DirectoryInfo.MoveTo 有时会抛出 IOException
C# DirectoryInfo.MoveTo for Renaming Sometimes throws IOException
我正在尝试使用 DirectoryInfo.MoveTo() 重命名我刚刚将 Zip 存档解压缩到的目录。大多数情况下,该操作会抛出错误为 Access to the path '...' is denied.
的 IOException。有时,该操作有效,但我无法关联支持此操作的任何条件。我检查了许多论坛帖子和 Whosebug 问题,但我仍然无法正常工作。我确定它不是计算机权限。所有这些文件和文件夹都具有完整的 read/write 权限,我已经 运行 以管理员身份尝试了该程序。
这是我的代码:
// Compute directory names
string directoryPathWithPrefix = Path.Combine(this.OutputDirectory.FullName,
"TEMP_ " + Path.GetFileNameWithoutExtension(compressedData.FullName));
string directoryPathWithoutPrefix = Path.Combine(this.OutputDirectory.FullName,
Path.GetFileNameWithoutExtension(compressedData.FullName));
// Extract file to new directory
ZipFile.ExtractToDirectory(compressedData.FullName, directoryPathWithPrefix);
// Add tag for UploadId
File.Create(Path.Combine(directoryPathWithPrefix,
upload.UploadId.ToString() + ".UPLOADID")).Close();
// Rename file
DirectoryInfo oldDir = new DirectoryInfo(directoryPathWithPrefix);
oldDir.MoveTo(directoryPathWithoutPrefix);
我试过使用 Process Explorer 来监视目录的句柄,但未能从中找到任何有用的数据。使用 Directory.Move() 或在 using
块内创建 ZipArchive 对象仍然会引发错误。
我真的被这个难住了。请帮忙。
澄清:
我是 运行 Windows 7,这个程序是在 .NET 4.5
下构建的
这是我收到的错误:
System.IO.IOException occurred
HResult=-2146232800
Message=Access to the path '{PATH}' is denied.
Source=mscorlib
StackTrace:
at System.IO.DirectoryInfo.MoveTo(String destDirName)
at DataImporter.ImportFDUU(FileSystemInfo uploadToImport, Message& reportMessage) in {CODE FILE}:line 380
InnerException:
运行 目录 returns 上的 cacls 此信息:
BUILTIN\Administrators:(OI)(CI)F
{MY USER}:(OI)(CI)F
我终于弄明白了。 ZipFile.ExtractToDirectory()
存在一些奇怪的故障,当它完成提取时,不会释放对目录中文件的访问权限。为了解决这个问题,我更改了我的代码如下:
- 将文件提取到临时目录。
- 将文件从临时目录复制到目标目录
- 删除临时目录
// 计算目录名
字符串 directoryPathWithPrefix = Path.Combine(this.SeparatorInputDirectory.FullName,
"TEMP_ " + Path.GetFileNameWithoutExtension(compressedFlightData.FullName));
string directoryPathWithoutPrefix = Path.Combine(this.SeparatorInputDirectory.FullName,
Path.GetFileNameWithoutExtension(compressedFlightData.FullName));
string tempDirectoryPath = Path.Combine(TempDirectoryPath, Path.GetDirectoryName(directoryPathWithoutPrefix));
// Extract file to new directory in the separator input directory
ZipFile.ExtractToDirectory(compressedFlightData.FullName, tempDirectoryPath);
// Add tag for UploadId
File.Create(Path.Combine(tempDirectoryPath,
upload.UploadId.ToString() + ".UPLOADID")).Close();
// Rename file to trigger separation
DirectoryCopy(tempDirectoryPath, directoryPathWithPrefix);
Directory.Move(directoryPathWithPrefix, directoryPathWithoutPrefix);
public static void DirectoryCopy(string sourceDirName, string destDirName)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
file.CopyTo(tempPath, false);
}
// Copy subdirectories and their contents to new location.
foreach (DirectoryInfo subDir in dirs)
{
string tempPath = Path.Combine(destDirName, subDir.Name);
DirectoryCopy(subDir.FullName, tempPath);
}
}
我正在尝试使用 DirectoryInfo.MoveTo() 重命名我刚刚将 Zip 存档解压缩到的目录。大多数情况下,该操作会抛出错误为 Access to the path '...' is denied.
的 IOException。有时,该操作有效,但我无法关联支持此操作的任何条件。我检查了许多论坛帖子和 Whosebug 问题,但我仍然无法正常工作。我确定它不是计算机权限。所有这些文件和文件夹都具有完整的 read/write 权限,我已经 运行 以管理员身份尝试了该程序。
这是我的代码:
// Compute directory names
string directoryPathWithPrefix = Path.Combine(this.OutputDirectory.FullName,
"TEMP_ " + Path.GetFileNameWithoutExtension(compressedData.FullName));
string directoryPathWithoutPrefix = Path.Combine(this.OutputDirectory.FullName,
Path.GetFileNameWithoutExtension(compressedData.FullName));
// Extract file to new directory
ZipFile.ExtractToDirectory(compressedData.FullName, directoryPathWithPrefix);
// Add tag for UploadId
File.Create(Path.Combine(directoryPathWithPrefix,
upload.UploadId.ToString() + ".UPLOADID")).Close();
// Rename file
DirectoryInfo oldDir = new DirectoryInfo(directoryPathWithPrefix);
oldDir.MoveTo(directoryPathWithoutPrefix);
我试过使用 Process Explorer 来监视目录的句柄,但未能从中找到任何有用的数据。使用 Directory.Move() 或在 using
块内创建 ZipArchive 对象仍然会引发错误。
我真的被这个难住了。请帮忙。
澄清: 我是 运行 Windows 7,这个程序是在 .NET 4.5
下构建的这是我收到的错误:
System.IO.IOException occurred
HResult=-2146232800
Message=Access to the path '{PATH}' is denied.
Source=mscorlib
StackTrace:
at System.IO.DirectoryInfo.MoveTo(String destDirName)
at DataImporter.ImportFDUU(FileSystemInfo uploadToImport, Message& reportMessage) in {CODE FILE}:line 380
InnerException:
运行 目录 returns 上的 cacls 此信息:
BUILTIN\Administrators:(OI)(CI)F
{MY USER}:(OI)(CI)F
我终于弄明白了。 ZipFile.ExtractToDirectory()
存在一些奇怪的故障,当它完成提取时,不会释放对目录中文件的访问权限。为了解决这个问题,我更改了我的代码如下:
- 将文件提取到临时目录。
- 将文件从临时目录复制到目标目录
- 删除临时目录
// 计算目录名 字符串 directoryPathWithPrefix = Path.Combine(this.SeparatorInputDirectory.FullName, "TEMP_ " + Path.GetFileNameWithoutExtension(compressedFlightData.FullName));
string directoryPathWithoutPrefix = Path.Combine(this.SeparatorInputDirectory.FullName,
Path.GetFileNameWithoutExtension(compressedFlightData.FullName));
string tempDirectoryPath = Path.Combine(TempDirectoryPath, Path.GetDirectoryName(directoryPathWithoutPrefix));
// Extract file to new directory in the separator input directory
ZipFile.ExtractToDirectory(compressedFlightData.FullName, tempDirectoryPath);
// Add tag for UploadId
File.Create(Path.Combine(tempDirectoryPath,
upload.UploadId.ToString() + ".UPLOADID")).Close();
// Rename file to trigger separation
DirectoryCopy(tempDirectoryPath, directoryPathWithPrefix);
Directory.Move(directoryPathWithPrefix, directoryPathWithoutPrefix);
public static void DirectoryCopy(string sourceDirName, string destDirName)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
file.CopyTo(tempPath, false);
}
// Copy subdirectories and their contents to new location.
foreach (DirectoryInfo subDir in dirs)
{
string tempPath = Path.Combine(destDirName, subDir.Name);
DirectoryCopy(subDir.FullName, tempPath);
}
}