2

I'm trying to delete every folder that contains the user's "user name" and it's contents located in C:\Users\User like so:

    foreach (var subdir in directory.GetDirectories().Where(subdir => subdir.Name.ToLower().Contains(Environment.UserName))) {
        try {
            Directory.Delete(subdir.FullName, true);
        } catch (Exception exception) {
            Console.Write("Deleting " + subdir.FullName + " caused exception: \n" + exception);
        }
    }

When I try to run the Windows Form binary, I get a 'System.UnauthorizedAccessException' occurred in mscorlib.dll error when it hits the first couple of files. Here's the thing, I'm running it as an admin, I can delete those files in explorer without an issue (or even a UAC prompt), and there is not a process locking/using those files.

What's going on?

Amorphous
  • 675
  • 1
  • 8
  • 26
  • is this a windows or a web application you're running it from if web then IIS_USER needs same read/write permissions as you do under your user account – MethodMan Aug 19 '14 at 20:56
  • Sorry, it's a windows form application. Edit: DJ Craze is an awesome DJ. Not sure if your username is paying homage to him. – Amorphous Aug 19 '14 at 20:58
  • 1
    I am a DJ and I am glad that you recognize my talents off the clock..LOL – MethodMan Aug 19 '14 at 21:00
  • have you debugged this..? what is the value of `subdir` when you first step thru the foreach loop are you sure you're not trying to delete the entire Directory vs the FilePath + FileName..? – MethodMan Aug 19 '14 at 21:02
  • are any of the files read-only? Directory.Delete doesn't allow this – ajg Aug 19 '14 at 21:04
  • It's C:\Users\user1\user1_cache, and yes I am trying to delete the full directory. The true boolean in Directory.Delete(path, r) makes the call recursive. – Amorphous Aug 19 '14 at 21:04
  • perhaps you could try deleting the files recursively then Delete the folder.. what if there is a file in use..? you can't delete it then – MethodMan Aug 19 '14 at 21:06
  • @DJKRAZE, I made sure to programatically kill the only process using these files. Directory.Delete does delete files when used recursively. – Amorphous Aug 19 '14 at 21:07
  • @ajg, I really don't think they're read-only, but I'll double check. – Amorphous Aug 19 '14 at 21:08
  • if you look on MSDN this is what your error states `UnauthorizedAccessException The caller does not have the required permission.` are you running locally or under a windows account..? make sure the machine that you are running it from truly has the correct permissions simple as that – MethodMan Aug 19 '14 at 21:09
  • @DJKRAZE, doesn't work with my user (admin priv) and as admin. – Amorphous Aug 19 '14 at 21:10
  • Found another question with the same problem [System.UnauthorizedAccessException: Access to the path denied](http://stackoverflow.com/questions/8821410/system-unauthorizedaccessexception-access-to-the-path-denied) – MicroVirus Aug 19 '14 at 21:33

2 Answers2

6

swap directory.delete for this call

//Directory.Delete alternative
public void DeleteDirectory(string targetDir)
{
    File.SetAttributes(targetDir, FileAttributes.Normal);

    string[] files = Directory.GetFiles(targetDir);
    string[] dirs = Directory.GetDirectories(targetDir);

    foreach (string file in files)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }

    foreach (string dir in dirs)
    {
        DeleteDirectory(dir);
    }

    Directory.Delete(targetDir, false);
}
ajg
  • 1,743
  • 12
  • 14
  • That worked. Any idea why Directory.Delete() didn't? – Amorphous Aug 19 '14 at 21:15
  • 1
    read-only or some other file attribute - all this does is remove them first (File.SetAttributes). Daft isn't it to be able to strip the attributes then do the same! – ajg Aug 19 '14 at 21:18
  • Well, to be fair, it does say so in the documentation [here](http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx) that it will throw an exception on read-only files. – MicroVirus Aug 19 '14 at 21:31
  • it may due to a hidden file named "thumb.db". Check my post for more details :) – nevets Aug 19 '14 at 22:48
1

Actually there's another reason for this to happen: within the directory you may have a hidden irritating file named "Thumb.db", which contains thumbnail information of all your files. Sometimes this file won't get closed unless you terminate your explorer.exe via Task Manager or shut down your computer, and result in a "un-deletable" folder.

To get rid of this annoying piece, follow the instructions here :)

nevets
  • 4,631
  • 24
  • 40