private static void TraverseDirDFS(DirectoryInfo dir, Folder folder) { try { var files = dir.GetFiles(); foreach (var file in files) { var myFile = new File(file.Name, file.Length); folder.Files.Add(myFile); } var dirs = dir.GetDirectories(); foreach (var d in dirs) { var newFolder = new Folder(d.Name); TraverseDirDFS(d, newFolder); folder.Folders.Add(newFolder); } } catch (UnauthorizedAccessException) { } }
private static decimal CalculateSizeOfSubtreeWithRoot(Folder root) { decimal currentSize = 0; File[] files = root.Files; Folder[] folders = root.ChildFolders; foreach (var item in files) { currentSize += item.Size; } if (folders.Count() == 0) { return currentSize; } foreach (var item in folders) { try { currentSize += CalculateSizeOfSubtreeWithRoot(item); } catch { } } return currentSize; }
public static void Main() { var entryPoint = new Folder(GlobalConstants.FolderName, GlobalConstants.WindowsTraversePathForFiles); GetFolders(entryPoint); var result = new StringBuilder(); FileSystem(entryPoint, result, 0); Console.WriteLine(result); }
static void Main() { // to shorten the time. you can try it with C:\Windows too, but it will take some time var entryPoint = new Folder("Help", @"C:\Windows\Help"); GetSubFolders(entryPoint); StringBuilder sb = new StringBuilder(); GetFileSystemString(entryPoint, sb, 0); System.Console.WriteLine(sb); }
public static void Main() { // using the Help folder to shorten the time. // you can try it with C:\Windows too, but it will take some time var entryPoint = new Folder("Help", @"C:\Windows\Help"); GetSubFolders(entryPoint); StringBuilder result = new StringBuilder(); GetFileSystemString(entryPoint, result, 0); Console.WriteLine(result); }
public static void Main() { string rootFolderPath = @"C:/Windows"; var rootDirectory = new DirectoryInfo(rootFolderPath); var rootFolder = new Folder(); Console.Write(value: "Building folder tree"); BuildTree(rootDirectory, rootFolder); Console.WriteLine(); Console.WriteLine($"{rootFolderPath} size in GB excluding forbidden access folders:{Math.Round(CalculateSizeOfSubtreeWithRoot(rootFolder) / (decimal)Math.Pow(2, 30), 2)} GB"); }
private static void GetSubFolders(Folder folder) { foreach (var file in folder.Directory.GetFiles()) { folder.Files.Add(new File(file.Name, file.Length)); } foreach (var subDir in folder.Directory.GetDirectories()) { var subFolder = new Folder(subDir.Name, subDir.FullName); folder.Folders.Add(subFolder); GetSubFolders(subFolder); } }
private static void GetFileSystemString(Folder folder, StringBuilder result, int depth) { string indent = new string('.', depth * 3); result.AppendLine(string.Format("{0}{1} size: {2}", indent, folder.Name, folder.GetSize())); foreach (var file in folder.Files) { result.AppendLine(string.Format(".{0}-{1} size: {2}", indent, file.Name, file.Size)); } foreach (var subFolder in folder.Folders) { GetFileSystemString(subFolder, result, depth + 1); } }
private static void BuildTree(DirectoryInfo rootDirectory, Folder rootFolder) { rootFolder.Name = rootDirectory.Name; IList<DirectoryInfo> subDirectories = rootDirectory.EnumerateDirectories().ToList(); IList<FileInfo> files = rootDirectory.EnumerateFiles().ToList(); rootFolder.Files = new File[files.Count()]; rootFolder.ChildFolders = new Folder[subDirectories.Count()]; for (int i = 0; i < files.Count(); i++) { rootFolder.Files[i] = new File { Name = files[i].Name, Size = files[i].Length }; } if (subDirectories.Count() == 0) { return; } for (int i = 0; i < subDirectories.Count(); i++) { rootFolder.ChildFolders[i] = new Folder(); try { BuildTree(subDirectories[i], rootFolder.ChildFolders[i]); } catch { } if (i != 0 && i % 100 == 0) { Console.Write(value: "."); } } }
void WalkDirectoryTree(DirectoryInfo root) { FileInfo[] fiFiles = null; DirectoryInfo[] subDirs = null; try { fiFiles = root.GetFiles("*.*"); } catch (UnauthorizedAccessException e) { } if (fiFiles != null) { this.files = new File[fiFiles.Length]; for (int i = 0; i < files.Length; i++) { files[i] = new File(); files[i].name = fiFiles[i].Name; files[i].size = (int)fiFiles[i].Length; sumOfAllFiles += fiFiles[i].Length; } } try { subDirs = root.GetDirectories(); } catch (UnauthorizedAccessException e) { } if (subDirs != null) { this.childFolders = new Folder[subDirs.Length]; for (int i = 0; i < childFolders.Length; i++) { childFolders[i] = new Folder(this.rootDir + "\\" + subDirs[i].Name); sumOfAllFiles += childFolders[i].GetFileSize(); } } }
static void Main(string[] args) { Folder windows = new Folder("C:\\WINDOWS"); Console.WriteLine(windows.GetFileSize()); }