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?