With the current console application we display the contents of a given folder.
It is a little useless by itself, but the code has one good advantage – it shows perfect application of DirectoryInfo and some properties as .FullName & .GetDirectories.
So, in general, if you want to see the contents of any folder, simply change the line “C:\\DOWNLOADS” at the bottom lines to any other tree you may like to explore. In order to escape the “\” symbol in C#, you should double it. Thus instead of “C:\Windows”, you should write “C:\\Windows”.
Furthermore, every depth level of the given directory starts with a little more on the right.
Here is the code:
public static class DirectoryTraverserDFS
private static void TraverseDir(DirectoryInfo dir, string spaces)
Console.WriteLine(spaces + dir.FullName);
DirectoryInfo children = dir.GetDirectories();
foreach (DirectoryInfo child in children)
TraverseDir(child, spaces + " ");
public static void TraverseDir(string directoryPath)
TraverseDir(new DirectoryInfo(directoryPath), string.Empty);
public static void Main()
Some of the code of the current article is part of the free Bulgarian C# programming book.