Beispiel #1
0
        private static void FillDirectoryTree(string path, Folder folder)
        {
            try
            {
                // add files
                var files = Directory.GetFiles(path);
                foreach (var file in files)
                {
                    string fileName = GetName(file);
                    FileInfo fileInfo = new FileInfo(file);
                    folder.AddFile(new File(fileName, fileInfo.Length));
                }

                // repeat for nested directories
                var directories = Directory.GetDirectories(path);
                foreach (var dir in directories)
                {
                    string dirName = GetName(dir);
                    Folder newFolder = new Folder(dirName);
                    folder.AddFolder(newFolder);
                    FillDirectoryTree(dir, newFolder);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Beispiel #2
0
 static void Main()
 {
     string rootDir = @"D:\desktop";
     Folder winFolder = new Folder("desktop");
     FillDirectoryTree(rootDir, winFolder);
     PrintSize(winFolder);
 }
        private void Populate(string root)
        {
            var dirs = new List<Folder>();
            var files = new List<File>();
            foreach (var directory in Directory.GetDirectories(root))
            {
                try
                {
                    var path = Path.Combine(root, directory);
                    var folder = new Folder(path);
                    dirs.Add(folder);
                    var filenames = Directory.GetFiles(path);
                    if (filenames.Length > 0)
                    {
                        foreach (var filename in filenames)
                        {
                            FileInfo info = new FileInfo(Path.Combine(path, filename));
                            File f = new File(info.Name, info.Length);
                            files.Add(f);
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                }

                this.Files = files.ToArray();
            }
            this.ChildFolders = dirs.ToArray();
        }
Beispiel #4
0
        public static Folder GetFolderTree(DirectoryInfo directoryInfo)
        {
            Folder currentFolder = new Folder(directoryInfo.Name);

            try
            {
                FileInfo[] currentFiles = directoryInfo.GetFiles();
                for (int i = 0; i < currentFiles.Length; i++)
                {
                    File nextFile = new File(currentFiles[i].Name, ((decimal)currentFiles[i].Length) / 1048576);
                    currentFolder.AddFile(nextFile);
                }

                DirectoryInfo[] currentDirectories = directoryInfo.GetDirectories();
                for (int i = 0; i < currentDirectories.Length; i++)
                {
                    Folder nextFolder = GetFolderTree(currentDirectories[i]);
                    currentFolder.AddFolder(nextFolder);
                }

                return currentFolder;
            }
            catch (UnauthorizedAccessException)
            {
                return currentFolder;
            }
        }
Beispiel #5
0
        private static void CalculateSize(Folder folder, ref long totalSize)
        {
            foreach (var file in folder.Files)
            {
                totalSize += file.Size;
            }

            foreach (var dir in folder.ChildFolders)
            {
                CalculateSize(dir, ref totalSize);
            }
        }
 public static long GetFilesSizesSum(Folder folder)
 {
     long result = 0;
     if (folder.Files!=null)
     {
         result = folder.Files.Sum(x => x.Size);
     }
     
     foreach (var f in folder.ChildFolders)
     {
         if (folder.Files!=null)
         {
             result += GetFilesSizesSum(f);
         }
        
     }
     return result;
 }
        static void Main(string[] args)
        {
            const string RootDir = @"c:\windows";
            Console.WriteLine("Populating folders, please wait..");
            var folder = new Folder(RootDir);
            Console.WriteLine("Done!");

            Console.WriteLine("The sum of the filesizes in folder {0} is {1}", RootDir, Folder.GetFilesSizesSum(folder));

            for (int i = 0; i < folder.ChildFolders.Length; i++)
            {
                if (folder.ChildFolders[i].Name == "c:\\windows\\System32")
                {
                    long size = Folder.GetFilesSizesSum(folder.ChildFolders[i]);
                    Console.WriteLine("The sum of the filesizes in folder {0} is {1}", folder.ChildFolders[i].Name, size);
                }
            }
        }
Beispiel #8
0
 public void AddFolder(Folder folderToAdd)
 {
     this.folders.Add(folderToAdd);
 }
Beispiel #9
0
 private static long GetSize(Folder folder)
 {
     long totalSize = 0;
     CalculateSize(folder, ref totalSize);
     return totalSize;
 }
Beispiel #10
0
 private static void PrintSize(Folder folder)
 {
     long folderSize = GetSize(folder);
     Console.WriteLine("Total size of {0} folder is {1} bytes.", folder.Name, folderSize);
 }
Beispiel #11
0
 public void AddFolder(Folder folder)
 {
     this.folders.Add(folder);
 }