2016-07-18 47 views
0

我想使用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 
+0

您是否验证过,没有真正的访问冲突?您可以使用[Cacls](https://technet.microsoft.com/en-us/library/bb490872.aspx)或[Icacls](https://technet.microsoft.com/en-us/library/cc753525。 aspx)显示任何给定文件的DACL。 – IInspectable

+0

@IInspectable我只是用该命令返回的信息更新了帖子。 –

回答

0

我终于明白了这个问题。 ZipFile.ExtractToDirectory()存在一些奇怪的故障,它在完成解压缩后不释放对目录中文件的访问。为了解决这个问题,我改变了我的代码,如下所示:

  1. 将文件解压到一个临时目录。
  2. 复制从临时目录到目标目录中的文件
  3. 删除临时目录

//计算目录名 串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); 
    } 
} 
相关问题