Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to list all sub directories in a directory

Tags:

c#

directory

I'm working on a project and I need to list all sub directories in a directory for example how could I list all the sub directories in c:\

like image 239
Matthewj Avatar asked Sep 04 '11 01:09

Matthewj


People also ask

How do I list all subfolders in a directory?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.

How do you list all files directories and all sub files or sub directories in one command?

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

Which command will find all the subdirectories within directories?

To Search Subdirectories To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.


2 Answers

Use Directory.GetDirectories to get the subdirectories of the directory specified by "your_directory_path". The result is an array of strings.

var directories = Directory.GetDirectories("your_directory_path"); 

By default, that only returns subdirectories one level deep. There are options to return all recursively and to filter the results, documented here, and shown in Clive's answer.


Avoiding an UnauthorizedAccessException

It's easily possible that you'll get an UnauthorizedAccessException if you hit a directory to which you don't have access.

You may have to create your own method that handles the exception, like this:

public class CustomSearcher {      public static List<string> GetDirectories(string path, string searchPattern = "*",         SearchOption searchOption = SearchOption.AllDirectories)     {         if (searchOption == SearchOption.TopDirectoryOnly)             return Directory.GetDirectories(path, searchPattern).ToList();          var directories = new List<string>(GetDirectories(path, searchPattern));          for (var i = 0; i < directories.Count; i++)             directories.AddRange(GetDirectories(directories[i], searchPattern));          return directories;     }      private static List<string> GetDirectories(string path, string searchPattern)     {         try         {             return Directory.GetDirectories(path, searchPattern).ToList();         }         catch (UnauthorizedAccessException)         {             return new List<string>();         }     } } 

And then call it like this:

var directories = CustomSearcher.GetDirectories("your_directory_path"); 

This traverses a directory and all its subdirectories recursively. If it hits a subdirectory that it cannot access, something that would've thrown an UnauthorizedAccessException, it catches the exception and just returns an empty list for that inaccessible directory. Then it continues on to the next subdirectory.

like image 123
Grant Winney Avatar answered Sep 19 '22 08:09

Grant Winney


Easy as this:

string[] folders = System.IO.Directory.GetDirectories(@"C:\My Sample Path\","*", System.IO.SearchOption.AllDirectories); 
like image 23
Clive Avatar answered Sep 20 '22 08:09

Clive