简体   繁体   中英

Get all subfolders without using pre-built .net objects

I am trying to get a list of all the folders in the c:\\Users\\UserName directory of my W7 machine.

The issue I have when I use Directory.GetDirectories() or Directory.GetFiles() is that I often get exceptions that "access to the path is denied". I have ran VS in administrator mode and had no luck. The reason why is explained in my C# - Cannot access all files

I would like to know how to get the names of all subfolders (regardless of whether there are any files in the folder or not) where the subfolders could be N deep (so, undetermined number of subfolders) and store them as a List. This way, via a foreach loop, I can check on a folder by folder basis against certain logic to ensure I have access and that I want access!

The following code only looks at 1 level deep:

 private List<string> GetAllFolders()
 {
     DirectoryInfo directoryInfo = new DirectoryInfo(this.sourceFolder);
     List<string> allFolders = new List<string>();

     foreach (DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories())
     {
         //logic
         allFolders.Add(subDirectoryInfo.FullName);
     }
     return allFolders;
 }

Is there a pattern I can use for this, or does any one have an example or a suggestion on how to proceed?

You are almost there.

private List<string> GetAllFolders()
{
    DirectoryInfo directoryInfo = new DirectoryInfo(this.sourceFolder);
    List<string> allFolders = new List<string>();

    foreach (DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories(("*.*", SearchOption.AllDirectories))
    {
        //logic
        allFolders.Add(subDirectoryInfo.FullName);
    }
    return allFolders;
}

Regarding access rights, you might not be able to access to the end of every folder, but I think that happens with certain special folders, something you have to handle yourself somehow. Otherwise this works fine.

Additionally see this: Best way to iterate folders and subfolders

I didn't run this code but you have to recursively iterate over the folders. I added no exception handling, except for 'catch and continue'

private List<string> GetAllFolders()
{
   var allFolders = new List<string>();
   return GetAllFolders(this.sourceFolder);  
}

// recursively list all folders, catch and continue in case of errors
private List<string> GetAllFolders(string folder)
{
     DirectoryInfo directoryInfo = new DirectoryInfo(folder);
     List<string> allFolders = new List<string>();

     foreach (DirectoryInfo subDirectoryInfo in directoryInfo.GetDirectories())
     {
          //logic
         try {
           allFolders.Add(subDirectoryInfo.FullName);
          allFolders.AddRange(GetAllFolders(subDirectoryInfo.FullName));
        }
        catch (Exception exp)
        {
          // log rrors
          Debug.WriteLine(" exception for " + 
                              subDirectoryInfo.FullName + " : " + 
                            exp.Message);
                } 
            }
            return allFolders;
        }
public void GetFolderList()
{
    var list = new List<string>();
    var root = new DirectoryInfo("c:\\");
    GetFoldersRecursive(root, list);
}

private static void GetFoldersRecursive(DirectoryInfo root, List<string> list)
{
    DirectoryInfo[] children;
    try
    {
        list.Add(root.FullName);
        children = root.GetDirectories();
    }
    catch(UnauthorizedAccessException t)
    {
        // access denied
        return;
    }

    foreach (var d in children)
        GetFoldersRecursive(d, list);
}

If you prefer to use a more sophisticated permission check (than catching exceptions) I suggest to have a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM