static void Main(string[] args) { // parse optional command line arguments if (args != null) { // show info for subdirectories argAllDirs = args.Contains("alldirs"); // show info in a form with tree view argShowTree = args.Contains("showtree"); } // read user folder input directory Console.WriteLine("Enter directory to scan:"); var directoryToScan = Console.ReadLine(); directoryToScan = Path.GetFullPath(String.IsNullOrEmpty(directoryToScan) ? appDir : directoryToScan); try { // nonexistent directory if (!Directory.Exists(directoryToScan)) { Console.WriteLine($"Directory: {directoryToScan} does not exist."); } // process the directory else { Console.WriteLine($"Scanning directory: {directoryToScan}{Environment.NewLine}"); // create folder data var folderData = new FolderData(directoryToScan); // print results PrintFolderInfo(folderData); // optinally show tree view if (argShowTree) { using (var folderDataView = new FolderDataView(folderData)) { folderDataView.ShowDialog(); } } } } catch (Exception exc) { Console.WriteLine($"Error occured:{Environment.NewLine}{exc.Message}"); } // close program Console.WriteLine(Environment.NewLine + "Press any key to exit."); Console.ReadKey(); }
/// <summary> /// Prints computed folder info in a console /// </summary> /// <param name="folderData">Folder data</param> private static void PrintFolderInfo(FolderData folderData) { Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine($"Folder: {folderData.Directory}"); Console.ResetColor(); Console.WriteLine($"{"Size:", PropertyAlignment}{folderData.Size, ValueAlignment}"); Console.WriteLine($"{"Size Without SubFolders:", PropertyAlignment}{folderData.SizeWithoutSubFolders, ValueAlignment}"); Console.WriteLine($"{"Folder Count:", PropertyAlignment}{folderData.FolderCount, ValueAlignment}"); Console.WriteLine($"{"Min Folder Size:", PropertyAlignment}{folderData.MinFolderSize, ValueAlignment}"); Console.WriteLine($"{"Max folder Size:", PropertyAlignment}{folderData.MaxFolderSize, ValueAlignment}"); Console.WriteLine($"{"Average Folder size:", PropertyAlignment}{folderData.AverageFolderSize, ValueAlignment}"); // print subdirectories if command switch is set if (argAllDirs) { foreach (var subFolderData in folderData.SubFolderData) { PrintFolderInfo(subFolderData); } } }
/// <summary> /// Populates tree view with folder data /// </summary> /// <param name="nodes">Nodes to be filled with folder data</param> /// <param name="folderData">Folder data</param> private void PopulateTreeView(TreeNodeCollection nodes, FolderData folderData) { try { var node = new TreeNode(folderData.Directory); nodes.Add(node); node.BackColor = SystemColors.GradientActiveCaption; nodes.Add($"Size: {folderData.Size}"); nodes.Add($"SizeWithoutSubFolders: {folderData.SizeWithoutSubFolders}"); nodes.Add($"FolderCount: {folderData.FolderCount}"); nodes.Add($"MinFolderSize: {folderData.MinFolderSize}"); nodes.Add($"MaxFolderSize: {folderData.MaxFolderSize}"); nodes.Add($"AverageFolderSize: {folderData.AverageFolderSize}"); foreach (var subFolderData in folderData.SubFolderData) { PopulateTreeView(node.Nodes, subFolderData); } } catch (Exception exc) { MessageBox.Show($"Unable to show folder data tree:{Environment.NewLine}{exc.Message}", "Folder Data Tree View", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
/// <summary> /// Computes folder data, including subdirectories using recursion /// </summary> /// <param name="directory">Folder directory to be computed</param> private void ComputeData(string directory) { long folderSize = 0; folderCount++; folderSize = GetDirectorySize(directory); sizeWithoutSubFolders = folderSize; size += folderSize; minFolderSize = folderSize; maxFolderSize = folderSize; // recursively process all subdirectories var directories = System.IO.Directory.GetDirectories(directory); foreach (var directoryItem in directories) { var subFolderDataItem = new FolderData(directoryItem); folderCount += subFolderDataItem.FolderCount; size += subFolderDataItem.Size; minFolderSize = minFolderSize < subFolderDataItem.minFolderSize ? minFolderSize : subFolderDataItem.minFolderSize; maxFolderSize = maxFolderSize > subFolderDataItem.maxFolderSize ? maxFolderSize : subFolderDataItem.maxFolderSize; subFolderData.Add(subFolderDataItem); } }
/// <summary> /// FolderDataView form constructor /// </summary> /// <param name="folderData">Folder data to be displayed in tree view</param> public FolderDataView(FolderData folderData) { InitializeComponent(); PopulateTreeView(folderDataTreeView.Nodes, folderData); }