Ejemplo n.º 1
0
        private void AddFilesToFolder(Folder root, string[] files)
        {
            List<File> currentDirectoryFiles = new List<File>();

            foreach (string file in files)
            {
                FileInfo info = new FileInfo(file);
                long size = info.Length;
                File currentFile = new File(file.ToString(), size);
                currentDirectoryFiles.Add(currentFile);
            }

            root.Files = currentDirectoryFiles.ToArray();
        }
Ejemplo n.º 2
0
        public void CalculateFolderSize(Folder folder, ref long sum)
        {
            if (folder == null)
            {
                throw new ArgumentNullException("Folder cannot be null!");
            }

            for (int i = 0; i < folder.Files.Length; i++)
            {
                if (folder.Files[i] != null)
                {
                    sum += folder.Files[i].Size;
                }
            }

            for (int i = 0; i < folder.ChildFoldersCount; i++)
            {
                Folder currentFolder = folder.GetChildFolderAtIndex(i);
                CalculateFolderSize(currentFolder, ref sum);
            }
        }
Ejemplo n.º 3
0
        private void AddSubfoldersToFolder(Folder root, IEnumerable<string> folders)
        {
            List<Folder> subfolders = new List<Folder>();
            foreach (var folder in folders)
            {
                Folder currentFolder = new Folder(folder);
                subfolders.Add(currentFolder);
            }

            root.ChildFolders = subfolders.ToArray();
        }
Ejemplo n.º 4
0
 public FolderHierarchy(Folder root)
 {
     this.Root = PopulateFileTree(root);
 }
Ejemplo n.º 5
0
        private Folder PopulateFileTree(Folder root)
        {
            string[] files = Directory.GetFiles(root.Name);
            IEnumerable<string> folders = Directory.EnumerateDirectories(root.Name);

            AddFilesToFolder(root, files);
            AddSubfoldersToFolder(root, folders);

            for (int i = 0; i < root.ChildFoldersCount; i++)
            {
                try
                {
                    root.ChildFolders[i] = PopulateFileTree(root.GetChildFolderAtIndex(i));
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine("{0} cannot be accessed!", root.GetChildFolderAtIndex(i));
                }
            }

            return root;
        }
Ejemplo n.º 6
0
        private Folder FindFolder(Folder start, string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("Path cannot be null");
            }

            if (start.Name == path)
            {
                return start;
            }

            for (int i = 0; i < start.ChildFoldersCount; i++)
            {
                Folder currentFolder = start.GetChildFolderAtIndex(i);
                FindFolder(currentFolder, path);
            }

            throw new ArgumentException("Folder not found in tree hierarchy!");
        }