Using Recursion to Get Directory Tree C# with Example
One of the uses of recursion is to navigate through a hierarchical data structure, like a file system directory tree, without knowing how many levels the tree has or the number of objects on each level. In this example, you will see how to use recursion on a directory tree to find all sub-directories of a specified directory and print the whole tree to the console. internal class Program { internal const int RootLevel = 0; internal const char Tab = '\t'; internal static void Main() { Console.WriteLine("Enter the path of the root directory:"); var rootDirectorypath = Console.ReadLine(); Console.WriteLine( $"Getting directory tree of '{rootDirectorypath}'"); PrintDirectoryTree(rootDirectorypath); Console.WriteLine("Press 'Enter' to quit..."); Console.ReadLine(); } internal static void PrintDirectoryTree(string rootDirectoryPath) { try { if (!Directory.Exists(rootDirectoryPath)) { throw new DirectoryNotFoundException( $"Directory '{rootDirectoryPath}' not found."); } var rootDirectory = new DirectoryInfo(rootDirectoryPath); PrintDirectoryTree(rootDirectory, RootLevel); } catch (DirectoryNotFoundException e) { Console.WriteLine(e.Message); } } private static void PrintDirectoryTree( DirectoryInfo directory, int currentLevel) { var indentation = string.Empty; for (var i = RootLevel; i < currentLevel; i++) { indentation += Tab; } Console.WriteLine($"{indentation}-{directory.Name}"); var nextLevel = currentLevel + 1; try { foreach (var subDirectory in directory.GetDirectories()) { PrintDirectoryTree(subDirectory, nextLevel); } } catch (UnauthorizedAccessException e) { Console.WriteLine($"{indentation}-{e.Message}"); } } } This code is somewhat more complicated than the bare minimum to complete this task, as it includes exception checking to handle any issues with getting the directories. Below you will find a break-down of the code into smaller segments with explanations of each. Main: The main method takes an input from a user as a string, which is to be used as the path to the root directory. It then calls the PrintDirectoryTree method with this string as the parameter. PrintDirectoryTree(string): This is the first of two methods that handle the actual directory tree printing. This method takes a string representing the path to the root directory as a parameter. It checks if the path is an actual directory, and if not, throws a DirectoryNotFoundException which is then handled in the catch block. If the path is a real directory, a DirectoryInfo object rootDirectory is created from the path, and the second PrintDirectoryTree method is called with the rootDirectory object and RootLevel, which is an integer constant with a value of zero. PrintDirectoryTree(DirectoryInfo, int): This second method handles the brunt of the work. It takes a DirectoryInfo and an integer as parameters. The DirectoryInfo is the current directory, and the integer is the depth of the directory relative to the root. For ease of reading, the output is indented for each level deep the current directory is, so that the output looks like this: -Root -Child 1 -Child 2 -Grandchild 2.1 -Child 3 Once the current directory is printed, its sub directories are retrieved, and this method is then called on each of them with a depth level value of one more than the current. That part is the recursion: the method calling itself. The program will run in this manner until it has visited every directory in the tree. When it reached a directory with no sub directories, the method will return automatically. This method also catches an UnauthorizedAccessException, which is thrown if any of the sub directories of the current directory are protected by the system. The error message is printed at the current indentation level for consistency. The method below provides a more basic approach to this problem: internal static void PrintDirectoryTree(string directoryName) { try { if (!Directory.Exists(directoryName)) return; Console.WriteLine(directoryName); foreach (var d in Directory.GetDirectories(directoryName)) { PrintDirectoryTree(d); } } catch (Exception e) { Console.WriteLine(e.Message); } } This does not include the specific error checking or output formatting of the first approach, but it effectively does the same thing. Since it only uses strings as opposed to DirectoryInfo, it cannot provide access to other directory properties like permissions.