Directory.Delete doesn't work. Access denied error but under Windows Explorer it's ok

Viewed 29723

I have searched the SO but find nothing.

Why this doesn't work?

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

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

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

6 Answers

Adding to @binball and @Chuck answer. Here is a somewhat quick async-friendly implementation of setAttributesNormal() using BFS to traverse directories. It's ideal for deep directory traversal, where recursion may fill call stack.

internal static void SetAttributesNormal(DirectoryInfo path) {
    // BFS folder permissions normalizer
    Queue<DirectoryInfo> dirs = new Queue<DirectoryInfo>();
    dirs.Enqueue(path);
    while (dirs.Count > 0) {
        var dir = dirs.Dequeue();
        dir.Attributes = FileAttributes.Normal;
        Parallel.ForEach(dir.EnumerateFiles(), e => e.Attributes = FileAttributes.Normal);
        foreach (var subDir in dir.GetDirectories())
            dirs.Enqueue(subDir);
    }
}
Related