private static Folder BuildDirectoryTree(string rootFolder) { try { var childFolders = Directory.GetDirectories(rootFolder); var filesInFolder = Directory.GetFiles(rootFolder); var currentFolder = new Folder(rootFolder, filesInFolder.Length, childFolders.Length); for (int i = 0; i < childFolders.Length; i++) { currentFolder.ChildFolders[i] = BuildDirectoryTree(childFolders[i]); } for (int i = 0; i < filesInFolder.Length; i++) { var fileName = filesInFolder[i].Substring(rootFolder.Length + 1); var fileInfo = new FileInfo(filesInFolder[i]); currentFolder.Files[i] = new File(fileName, fileInfo.Length); } return currentFolder; } catch (UnauthorizedAccessException e) { Console.WriteLine(e.Message); return new Folder(e.Message); } }
private static long GetFileSizeInFolderTree(Folder currentFolder) { var currentFileSize = 0L; foreach (var file in currentFolder.Files) { currentFileSize += file.Size; } foreach (var childFolder in currentFolder.ChildFolders) { currentFileSize += GetFileSizeInFolderTree(childFolder); } return currentFileSize; }
private static Folder GetSubTree(Folder root, string folderName) { if (root.Name.ToLower() == folderName.ToLower()) { return root; } var folderNameLength = folderName.Length; foreach (var child in root.ChildFolders) { if (child.Name.Length <= folderNameLength) { var folderNameStart = folderName.Substring(0, child.Name.Length); if (child.Name.ToLower() == folderNameStart.ToLower()) { return GetSubTree(child, folderName); } } } throw new DirectoryNotFoundException("Specified folder not found"); }