public static Folder CreateTreeByTraversingDirectory(DirectoryInfo directory)
        {
            var directoryFolder = new Folder(directory.Name);
            DirectoryInfo[] subDirectories = directory.GetDirectories();
            FileInfo[] allFiles = directory.GetFiles();

            foreach (var currentFile in allFiles)
            {
                directoryFolder.AddFile(new File(currentFile.Name, currentFile.Length));
            }

            try
            {
                foreach (var subdir in subDirectories)
                {
                    var resultSubDir = CreateTreeByTraversingDirectory(subdir);
                    directoryFolder.AddFolder(resultSubDir);
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e.Message);
            }

            return directoryFolder;
        }
        public static long CalculateSumOfFilesIn(Folder folder)
        {
            var root = folder;
            long sumOfFileSizes = 0;
            if (root.ChildFolders.Length > 0)
            {
                foreach (var childFolder in root.ChildFolders)
                {
                    var sizeOfCurrentChildFolder = CalculateSumOfFilesIn(childFolder);
                    sumOfFileSizes += sizeOfCurrentChildFolder;
                }
            }

            foreach (var file in root.Files)
            {
                sumOfFileSizes += file.Size;
            }

            return sumOfFileSizes;
        }
Exemplo n.º 3
0
        private void ResizeFolderContainer()
        {
            var doubleSize = this.childFolders.Length * 2;
            var doubleSizedFolderContainer = new Folder[doubleSize];
            for (int i = 0, len = this.childFolders.Length; i < len; i++)
            {
                doubleSizedFolderContainer[i] = this.childFolders[i];
            }

            this.childFolders = doubleSizedFolderContainer;
        }
Exemplo n.º 4
0
        public void AddFolder(Folder folder)
        {
            if (folder == null)
            {
                throw new ArgumentNullException("Folder can not be null!");
            }

            if (this.childFolders.Length == this.folderIndex)
            {
                ResizeFolderContainer();
            }

            this.childFolders[folderIndex] = folder;
            this.folderIndex++;
        }