Directory.Delete不起作用.访问被拒绝错误,但在Windows资源管理器中可以 [英] Directory.Delete doesn't work. Access denied error but under Windows Explorer it's ok

查看:44
本文介绍了Directory.Delete不起作用.访问被拒绝错误,但在Windows资源管理器中可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过SO,但是什么也没找到.

I have searched the SO but find nothing.

为什么这不起作用?

Directory.Delete(@"E:\3\{90120000-001A-0000-0000-0000000FF1CE}-C");

以上行将引发异常访问被拒绝".我具有管理员权限,可以使用Explorer删除目录.

Above line will throw exception "Access is denied". I have admin rigths and I can delete the dir with Explorer.

看起来像是一些禁止使用的字符?但是Windows资源管理器可以处理它.如何删除具有类似名称的目录?

It looks like some forbidden chars? but Windows Explorer can handle it. How can I delete directories with names like that?

推荐答案

感谢大家的投入,它有助于我快速找到解决方案.

Thank you all for your input, it helps me in quick find of solution.

Phil提到"Directory.Delete如果失败,无论权限如何,都会失败(请参阅msdn.microsoft.com/zh-cn/library/…的底部)"

As Phil mentioned "Directory.Delete fails if it is, regardless of permissions (see bottom of msdn.microsoft.com/en-us/library/…)"

此外无法从文件夹中删除只读属性微软说:

您可能无法删除文件夹中的只读属性使用Windows资源管理器.此外,某些程序可能显示错误您尝试将文件保存到时出现的消息文件夹.

You may be unable to remove the Read-Only attribute from a folder using Windows Explorer. In addition, some programs may display error messages when you try to save files to the folder.

结论:在删除之前,请始终删除所有与Normal不同的dir,file属性.所以下面的代码解决了这个问题:

Conclusion: always remove all dir,file attributes diffrent then Normal before deleting. So below code solve the problem:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"E:\3\{90120000-0021-0000-0000-0000000FF1CE}-C1");

if (dir.Exists)
{
    setAttributesNormal(dir);
    dir.Delete(true);
}

. . .

function setAttributesNormal(DirectoryInfo dir) {
    foreach (var subDir in dir.GetDirectories())
        setAttributesNormal(subDir);
    foreach (var file in dir.GetFiles())
    {
        file.Attributes = FileAttributes.Normal;
    }
}

这篇关于Directory.Delete不起作用.访问被拒绝错误,但在Windows资源管理器中可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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