private static void GetAllFilesAndFoldersInGivenDirectory(DirectoryInfo directory, Folder folder) { try { var files = directory.GetFiles(); foreach (var file in files) { var myFile = new File(); myFile.Name = file.Name; myFile.Size = file.Length; folder.Files.Add(myFile); } var directories = directory.GetDirectories(); foreach (var dir in directories) { var newFolder = new Folder(dir.Name); GetAllFilesAndFoldersInGivenDirectory(dir, newFolder); folder.ChildFolder.Add(newFolder); } } catch (UnauthorizedAccessException) { return; } }
private static ulong CalculateSumOfFileSizeInSubTree(Folder folder, ulong sum) { foreach (var file in folder.Files) { sum += (ulong)file.Size; } foreach (var subFolder in folder.ChildFolder) { sum += CalculateSumOfFileSizeInSubTree(subFolder, sum); } return sum; }