我想使用DirectoryInfo.MoveTo()重命名一个目录,我刚刚提取了一个Zip存档。大多数情况下,该操作抛出IOException,并显示错误Access to the path '...' is denied.
。有时候,该操作可以正常工作,但我无法关联任何支持此操作的情况。我已经检查了许多论坛帖子和StackOverflow关于这个错误的问题,但我仍然无法得到这个工作。我相信它不是电脑的权限。所有这些文件和文件夹都具有完全的读/写权限,我试图以管理员身份运行该程序。C#DirectoryInfo.MoveTo重命名有时抛出IOException
这里是我的代码:
// 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);
我已经使用进程资源管理器监视目录手柄尝试了,但一直没能从中找到任何有用的数据。使用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:
上的目录返回运行CACLS这信息:
BUILTIN\Administrators:(OI)(CI)F
{MY USER}:(OI)(CI)F
您是否验证过,没有真正的访问冲突?您可以使用[Cacls](https://technet.microsoft.com/en-us/library/bb490872.aspx)或[Icacls](https://technet.microsoft.com/en-us/library/cc753525。 aspx)显示任何给定文件的DACL。 – IInspectable
@IInspectable我只是用该命令返回的信息更新了帖子。 –