【问题标题】:"Access to the system path is denied" when using 'System.IO.Directory.Delete'使用“System.IO.Directory.Delete”时“拒绝访问系统路径”
【发布时间】:2011-01-11 21:51:39
【问题描述】:

我正在使用 System.IO.Directory.Delete 并尝试删除系统文件夹,例如“我的音乐”、“我的视频”等,但我收到类似于“访问系统路径 'C:\users\jbloggs\Saved 的错误游戏被拒绝”。但是,我可以毫无问题地通过资源管理器删除这些文件夹,我对这些文件夹拥有完全权限。有什么我可以尝试的建议吗?

我的代码:

public static void ClearAttributes(string currentDir)
{
    if (Directory.Exists(currentDir))
    {
        string[] subDirs = Directory.GetDirectories(currentDir);
        foreach (string dir in subDirs)
            ClearAttributes(dir);
        string[] files = files = Directory.GetFiles(currentDir);
        foreach (string file in files)
            File.SetAttributes(file, FileAttributes.Normal);
    }
}

用法:

try
{
    ClearAttributes(FolderPath);
    System.IO.Directory.Delete("C:\\users\\jbloggs\\Saved Games", true);
}
catch (IOException ex)
{
    MessageBox.Show(ex.Message);
}

【问题讨论】:

  • 没有相关的谷歌匹配到确切的字符串“访问系统路径”。这是错误消息的确切措辞吗?
  • 尝试引用您的路径字符串。使用@"C:\users\jbloggs\Saved Games"。另外,显示ex.ToString()。最后,这与 .NET 有关,与 C# 编程语言无关。
  • 属性并不总是问题。另一个原因是某些应用程序打开了该目录或它下面的东西。就我而言,应用程序是 Windows 资源管理器!它锁定了相关目录,即使“当前”文件夹位于我试图重命名的文件夹之外。在我关闭资源管理器之前,我的应用程序无法重命名目录。

标签: .net


【解决方案1】:

是的,该文件夹设置了“只读”属性。这会起作用:

var dir = new DirectoryInfo(@"c:\temp\test");
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
dir.Delete();

删除内容时应始终注意文件属性。一定要远离 System 或 ReparsePoint 的任何东西。小心 ReadOnly 和 Hidden。

【讨论】:

猜你喜欢
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2011-03-29
  • 1970-01-01
相关资源
最近更新 更多