尝试删除某个目录时拒绝访问该路径 [英] Access to the path is denied when trying to delete a certain directory

查看:216
本文介绍了尝试删除某个目录时拒绝访问该路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

这是我正在调试的某些C#代码的经过简化的伪代码版本。

Here's a grossly simplified pseudo-code version of some C# code that I'm debugging.

// 1) Lots of files and directories are copied

// 2) Some unnecessary files are deleted

// 3) Try to delete an unnecessary directory
string stubbornFolder = @"C:\...\Stubborn"; // This folder was created during step 1 above.
Directory.Delete(stubbornFolder);

问题

当它击中Directory.Delete时,将引发以下异常。

When it hits Directory.Delete, the following exception is thrown.

System.IO.IOException: Access to the path 'C:\...\Stubborn' is denied.

注释


  • 删除目录 C:... test也可以按预期进行。测试和顽固似乎都具有相同的安全设置。这两个目录都是完全空的。

  • Deleting the directory "C:...\test" also works as expected. test and Stubborn both appear to have the same security settings. Both directories are completely empty.

如果我手动删除,然后使用Windows资源管理器重新创建Stubborn(并使用调试器跳过复​​制代码),则删除

If I manually delete then re-create Stubborn using Windows Explorer (and skip over the copying code using a debugger), the deletion works as expected.


  • 即使在应用程序运行时手动删除也可以正常工作。

浏览访问控制类型,如这个问题似乎表明所有规则都设置为允许。

Peeking at the access control types like suggested in this question seemed to indicate that all rules were set to Allow.

不必要

以管理员身份运行可执行文件与以管理员身份运行可执行文件没有任何区别。

Running the executable as an admin vs. not as an admin doesn't make any difference.

似乎没有任何应用程序将 Stubborn 作为其工作目录。

There doesn't appear to be any applications that are using Stubborn as their working directory.

帮助!

任何可能引起问题的想法这个吗?

Any ideas on what might be causing this?

推荐答案

可能有某种属性阻止您删除文件夹。您可以先将属性设置为正常:

There may be some sort of attribute preventing you from deleting the folder. You might try setting the attributes to normal first:

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@"C:\...\Stubborn");
setAttributesNormal(directory);



void setAttributesNormal(System.IO.DirectoryInfo directory){
    foreach (string subDirectoryPath in directory.GetDirectories()){
        var directoryInfo = new DirectoryInfo(subDirectoryPath);
        foreach (string filePath in directoryInfo.GetFiles()) {
            var file = new FileInfo(filePath);
            file.Attributes = FileAttributes.Normal;
        }
    }
}

这篇关于尝试删除某个目录时拒绝访问该路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆