public void GetFilesAndChildFolders() { try { FolderNode currentFolder = this; string currentFolderPath = this.DirectoryPath; foreach (var filePath in Directory.GetFiles(currentFolderPath)) { FileInfo currentFileInfo = new FileInfo(filePath); FileNode currentFile = new FileNode(currentFileInfo.Name, currentFileInfo.Length); currentFolder.AddFile(currentFile); } foreach (var folderPath in Directory.GetDirectories(currentFolderPath)) { string subFolderName = Path.GetDirectoryName(folderPath + "\\"); FolderNode subFolder = new FolderNode(subFolderName); currentFolder.AddFolder(subFolder); subFolder.GetFilesAndChildFolders(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Main(string[] args) { string rootFolderPath = @"C:\Windows\"; string rootFolderName = Path.GetDirectoryName(rootFolderPath); FolderNode rootFolder = new FolderNode(rootFolderName); rootFolder.GetFilesAndChildFolders(); Console.WriteLine(rootFolder.FolderSize + " bytes"); var folder = rootFolder.GetChildFolder(@"C:\Windows\Fonts"); if (folder == null) { Console.WriteLine("Folder not found"); } else { Console.WriteLine(folder.FolderSize + " bytes"); } }