static void Main() { //Note this will work only if visual studio has the necessary permissions to open the directories //Alternatively test it in another folder //Do not foreget to include escapes in the startpath (i.e. C:\\Program Files) Folder startFolder = new Folder() { Name = "Windows" }; TraverseDir(folder: startFolder, startpath: "C:"); }
public static void TraverseDir(Folder folder, string startpath, string spaces = "") { string path = startpath + "\\" + folder.Name; DirectoryInfo dir = new DirectoryInfo(path); folder.Files = dir.GetFiles().Select(f => new File() { Name = f.Name, Size = f.Length }).ToArray(); folder.ChildFolders = dir.GetDirectories().Select(d => new Folder() { Name = d.Name }).ToArray(); Console.WriteLine(spaces + "=============================="); Console.WriteLine(spaces + "-> Folder name: " + folder.Name); Console.WriteLine(spaces + "-> Path: " + path); Console.WriteLine(spaces + "-> Size: " + CalculateFolderSize(path) + " bytes"); Console.WriteLine(spaces + "=============================="); foreach (var file in folder.Files) { Console.WriteLine(spaces + "||=>" + file.Name + " " + file.Size + " bytes"); } foreach (var childFolder in folder.ChildFolders) { TraverseDir(childFolder, path, spaces + " "); } }