Example #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);
            }
        }
Example #2
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);
            }
        }
Example #3
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;
            }
        }
Example #4
0
        private static void FillFolder(DirectoryInfo directory, Folder rootFolder)
        {
            var files = directory.GetFiles()
                        .Select(fInfo => new File(fInfo.Name, (int)fInfo.Length));

            rootFolder.AddFiles(files);

            foreach (var dir in directory.GetDirectories())
            {
                var folder = new Folder(dir.Name);
                rootFolder.AddFolder(folder);
                FillFolder(dir, folder);
            }
        }