C #: How to check if I can read and / or delete a directory

I recurse through a bunch of directories. Some of them (e.g. D: \ $ RECYCLE.BIN \ S-1-5-20) give me a System.UnauthorizedAccessException . I believe that I can just catch him and move on, but I would rather understand this in advance.

So, when I have a DirectoryInfo object. How can I see if I GetDirectories() allowed to GetDirectories() and possibly Delete() ?

+4
source share
4 answers

If you intend to delete it, try deleting it and then continue (handling the exception as necessary).

If you do check-and-then-delete-if-should-be-able-to-delete, there is a chance that the race condition in the file system will be insignificant. This applies to most file / directory access operations. Most file system operations are designed to be atomic and move this logic into user code, conflict with this atomicity, and still have to handle a possible exception.

+11
source

I built the following code. Please see if this helps:

 //using System.IO; //using System.Security.AccessControl; //using System.Security.Principal; string[] directories = Directory.GetDirectories( Path.Combine(Environment.CurrentDirectory, @"..\.."), "*", SearchOption.AllDirectories); foreach (string directory in directories) { DirectoryInfo info = new DirectoryInfo(directory); DirectorySecurity security = info.GetAccessControl(); Console.WriteLine(info.FullName); foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount))) { Console.WriteLine("\tIdentityReference = {0}", rule.IdentityReference); Console.WriteLine("\tInheritanceFlags = {0}", rule.InheritanceFlags ); Console.WriteLine("\tPropagationFlags = {0}", rule.PropagationFlags ); Console.WriteLine("\tAccessControlType = {0}", rule.AccessControlType); Console.WriteLine("\tFileSystemRights = {0}", rule.FileSystemRights ); Console.WriteLine(); } } 

Result:

  D: \ Projects \ ConsoleApplication1 \ bin
     IdentityReference = BUILTIN \ Administrators
     InheritanceFlags = ContainerInherit, ObjectInherit
     PropagationFlags = None
     AccessControlType = Allow
     FileSystemRights = FullControl

Note that the IdentityReference and FileSystemRights properties; you should probably check your current ACL against them before trying to delete the directory.

+4
source

I believe that you will need to write your own GetDirectories() method; which recursively gets those inside it.

There is a good article in this Microsoft article on how to do this, with a little work you can clear it to use shared lists and make it suitable for your solution.

Simply put, System.IO.Directory.GetDirectories () will fail every time it receives one of these exceptions.

A code like this (copied from above) should start working

  List<String> directories = new List<String>(); void DirSearch(string sDir) { try { foreach (string d in Directory.GetDirectories(sDir)) { //foreach (string f in Directory.GetFiles(d, txtFile.Text)) //{ // //} // use this to search for files recursivly. directories.Add(d); DirSearch(d); } } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } 

Once you have a list of directories, you can perform operations on them, with some mods, the above method should ensure that you have permission to read anything in the list.

0
source

I found this while trying to solve the problem ...

It seems like related, but rather spotless code, I have not tried it

http://technolog.nl/blogs/eprogrammer/archive/2009/06/18/Howto_3A00_-Properly-use-the-AccessCheck-API.aspx

considers

Mario

0
source

All Articles