private static int CalculateSize(Folder folder, int size = 0) { size += folder.Files.Sum(file => file.Size); size += folder.Folders.Sum(subfolder => CalculateSize(subfolder)); return size; }
public static void Main() { { // Windows directory is protected var rootPath = @"c:\windows"; var rootFolder = new Folder(rootPath); CreateFolderTree(rootFolder); var size = CalculateSize(rootFolder); } }
static void Main() { Console.WriteLine("Enter initial directory (default: c:\\windows)"); var rootPath = Console.ReadLine(); rootPath = rootPath == string.Empty ? @"C:\windows" : rootPath; var rootFolder = new Folder(rootPath); Console.WriteLine("Slow operation..."); CreateFolderTree(rootFolder); var size = CalculateSize(rootFolder); Console.WriteLine("{0} size: {1} bytes", rootPath, size); }
private static int CalculateSize(Folder folder, int size = 0) { foreach (var file in folder.Files) { size += file.Size; } foreach (var subfolder in folder.Folders) { size += CalculateSize(subfolder); } return size; }
public static void Main() { string rootPath = @"C:\Windows"; var rootFolder = new Folder(rootPath); try { CreateFolderTree(rootFolder); var size = CalculateSize(rootFolder); Console.WriteLine(@"The size of C:\Windows is {0} Mb", size); } catch (UnauthorizedAccessException e) { Console.WriteLine(e.Message); } }
private static void CreateFolderTree(Folder folder) { var folderInfo = new DirectoryInfo(folder.Name); var subFoldersToAdd = new List<Folder>(); var containedFiles = folderInfo.GetFiles(); folder.Files = containedFiles.Select(file => new File(file.Name, (int)file.Length)).ToArray(); foreach (var subfolder in Directory.GetDirectories(folder.Name)) { var folderToAdd = new Folder(subfolder); subFoldersToAdd.Add(folderToAdd); CreateFolderTree(folderToAdd); } folder.Folders = subFoldersToAdd.ToArray(); }
private static ulong CalculateSize(Folder folder, ulong size = 0) { foreach (var file in folder.Files) { size += (ulong)file.Size; } foreach (var subfolder in folder.Folders) { try { size += (ulong)CalculateSize(subfolder); } catch (System.Exception) { } } return size; }
private static void CreateFolderTree(Folder folder) { var folderInfo = new DirectoryInfo(folder.Name); var subFilesToAdd = new List<File>(); var subFoldersToAdd = new List<Folder>(); FileInfo[] containedFIlesInfo = new FileInfo[0]; try { containedFIlesInfo = folderInfo.GetFiles(); } catch (System.Exception) { } foreach (FileInfo file in containedFIlesInfo) { var fileToAdd = new File(file.Name, (int)file.Length); subFilesToAdd.Add(fileToAdd); } folder.Files = subFilesToAdd.ToArray(); try { var subDirs = Directory.GetDirectories(folder.Name); foreach (var subfolder in subDirs) { var folderToAdd = new Folder(subfolder); subFoldersToAdd.Add(folderToAdd); CreateFolderTree(folderToAdd); } folder.Folders = subFoldersToAdd.ToArray(); } catch (System.Exception) { } }