private void buttonAnalyse_Click(object sender, EventArgs e) { string path = textFilePath.Text; string folderName = (path.LastIndexOf('\\') + 1 < path.Length) ? path.Substring(path.LastIndexOf('\\') + 1) : path.Substring(0, path.LastIndexOf('\\') - 1); Folder rootFolder = new Folder(folderName, path); rootFolder.calculateSizes(); populateData(rootFolder); }
// Calculate the file sizes and total sizes public void calculateSizes() { string[] files = null; string[] subDirectories = null; try { files = Directory.GetFiles(Path); subDirectories = Directory.GetDirectories(Path); } catch (UnauthorizedAccessException e) { // For now, just console log inaccessible files (later create a proper log) System.Diagnostics.Debug.Write(e.Message); } catch (System.IO.DirectoryNotFoundException e) { System.Diagnostics.Debug.Write(e.Message); } if (files != null) { foreach (string file in files) { try { FileSize += new FileInfo(file).Length; } catch (FileNotFoundException e) { System.Diagnostics.Debug.Write(e.Message); } } // Iterate through the sub folders foreach (string subDirectory in subDirectories) { // Do not iterate through reparse points if ((File.GetAttributes(subDirectory) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { string folderName = (subDirectory.LastIndexOf('\\') + 1 < subDirectory.Length) ? subDirectory.Substring(subDirectory.LastIndexOf('\\') + 1) : subDirectory.Substring(0, subDirectory.LastIndexOf('\\') - 1); Folder newFolder = new Folder(folderName, subDirectory); newFolder.calculateSizes(); TotalSize += newFolder.TotalSize; // Store the new folder in its parent's subdirectory list Subdirectories.Add(newFolder); } } } TotalSize += FileSize; }