How to list all auxiliary directories in a directory

I am working on a project and I need to list all the subdirectories in the directory, for example, how can I list all the subdirectories in the c: \ directory

+77
c # directory
Sep 04 2018-11-11T00:
source share
5 answers

Use Directory.GetDirectories to get 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, this returns only subdirectories one level. There are options for returning all recursively and for filtering the results described here , and shown in Clive's answer.




UnauthorizedAccessException Prevention

It’s easy that you get an UnauthorizedAccessException if you end up in a directory that you don’t have access to.

You may need to create your own method that handles the exception, for example:

 public class CustomSearcher { public static List<string> GetDirectories(string path, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly) { 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 name it like this:

 var directories = CustomSearcher.GetDirectories("your_directory_path"); 
+121
04 Sep 2018-11-11T00:
source share

Just like this:

 string[] folders = System.IO.Directory.GetDirectories(@"C:\My Sample Path\","*", System.IO.SearchOption.AllDirectories); 
+50
Sep 04 2018-11-11T00:
source share
 FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult result = fbd.ShowDialog(); string[] files = Directory.GetFiles(fbd.SelectedPath); string[] dirs = Directory.GetDirectories(fbd.SelectedPath); foreach (string item2 in dirs) { FileInfo f = new FileInfo(item2); listBox1.Items.Add(f.Name); } foreach (string item in files) { FileInfo f = new FileInfo(item); listBox1.Items.Add(f.Name); } 
+6
Oct 21 '15 at 9:44
source share
 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TRIAL { public class Class1 { static void Main(string[] args) { string[] fileArray = Directory.GetDirectories("YOUR PATH"); for (int i = 0; i < fileArray.Length; i++) { Console.WriteLine(fileArray[i]); } Console.ReadLine(); } } } 
+5
Sep 24 '15 at 5:57
source share

show all directories and subdirectories

from global import glob dir = [] dir = glob ("path") def all_sub_dir (dir): {

  for item in dir: { b = "{}\*".format(item) dir += glob(b) } print(dir) 

}

-one
Nov 12 '18 at 16:50
source share



All Articles