Advertisement
sylviapsh

Build Windows Directories Tree

Jun 23rd, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. namespace BuildDirectoriesTree
  2. {
  3.     using System;
  4.     using System.IO;
  5.  
  6.     /// <summary>
  7.     /// Define classes File { string name, int size } and Folder { string name, File[] files, Folder[] childFolders } and using them build a tree keeping all files and folders on the hard drive starting from C:\WINDOWS. Implement a method that calculates the sum of the file sizes in given subtree of the tree and test it accordingly. Use recursive DFS traversal.
  8.     /// </summary>
  9.     public class BuildDirectoriesTree
  10.     {
  11.         public static void Main()
  12.         {
  13.             string rootDirectory = @"C:\Windows";
  14.             string dirName = "Windows";
  15.             Folder main = new Folder(dirName);
  16.             Folder tree = Tree.TraverseDirectory(rootDirectory, ref main);
  17.             long sum = Tree.CalculateFilesSum(tree);
  18.  
  19.             Console.WriteLine("The total size of the files is: {0} bytes", sum);
  20.         }
  21.     }
  22.    
  23.     public class File
  24.     {
  25.         public File(string name, long size)
  26.         {
  27.             this.Name = name;
  28.             this.Size = size;
  29.         }
  30.  
  31.         public File(string name) : this(name, 0)
  32.         {
  33.         }
  34.  
  35.         public string Name { get; set; }
  36.  
  37.         public long Size { get; set; }
  38.     }
  39.  
  40.     public class Folder
  41.     {
  42.         public Folder(string name)
  43.         {
  44.             this.Name = name;
  45.         }
  46.  
  47.         public string Name { get; set; }
  48.  
  49.         public File[] Files { get; set; }
  50.  
  51.         public Folder[] ChildFolders { get; set; }
  52.     }
  53.  
  54.     public class Tree
  55.     {
  56.         public static Folder TraverseDirectory(string path, ref Folder root)
  57.         {
  58.             DirectoryInfo directory = new DirectoryInfo(path);
  59.             string directoryName = directory.Name;
  60.             root = new Folder(directoryName);
  61.  
  62.             FileInfo[] currentDirectoryFiles = directory.GetFiles();
  63.             root.Files = new File[currentDirectoryFiles.Length];
  64.            
  65.             for (int i = 0; i < currentDirectoryFiles.Length; i++)
  66.             {
  67.                 string fileName = currentDirectoryFiles[i].Name;
  68.                 long length = currentDirectoryFiles[i].Length;
  69.  
  70.                 root.Files[i] = new File(fileName, length);
  71.             }
  72.  
  73.             DirectoryInfo[] currentDirectorySubDirectories = directory.GetDirectories();
  74.             root.ChildFolders = new Folder[currentDirectorySubDirectories.Length];
  75.            
  76.             for (int i = 0; i < currentDirectorySubDirectories.Length; i++)
  77.             {
  78.                 try
  79.                 {
  80.                     string dirName = currentDirectorySubDirectories[i].Name;
  81.                     path = currentDirectorySubDirectories[i].FullName;
  82.                     Folder currentSubFolder = new Folder(dirName);
  83.                     TraverseDirectory(path, ref currentSubFolder);
  84.                     root.ChildFolders[i] = currentSubFolder;                  
  85.                 }
  86.                 catch (System.UnauthorizedAccessException)
  87.                 {
  88.                     continue;
  89.                 }
  90.             }
  91.  
  92.             return root;
  93.         }
  94.  
  95.         public static long CalculateFilesSum(Folder folder)
  96.         {
  97.             long currentSum = 0;
  98.             foreach (var file in folder.Files)
  99.             {
  100.                 currentSum += file.Size;
  101.             }
  102.  
  103.             foreach (var childFolder in folder.ChildFolders)
  104.             {
  105.                 long childSum = CalculateFilesSum(childFolder);
  106.                 currentSum += childSum;
  107.             }
  108.  
  109.             return currentSum;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement