private static void TraverseDirDFS(DirectoryInfo directory, Folder folder)
        {
            try
            {
                var files = directory.GetFiles();
                foreach (var file in files)
                {
                    var myFile = new File(file.Name, file.Length);

                    folder.Files.Add(myFile);
                }

                var directories = directory.GetDirectories();
                foreach (var dir in directories)
                {
                    var newFolder = new Folder(dir.Name);

                    TraverseDirDFS(dir, newFolder);

                    folder.Folders.Add(newFolder);
                }
            }
            catch (UnauthorizedAccessException)
            {
                return;
            }
        }
Beispiel #2
0
        public static void Main()
        {
            var root = new Folder(StartFolder);

            FillFolderWithFiles(new DirectoryInfo(StartFolder), root);

            PrintFromFolder(root, 0);

            Console.WriteLine(TotalSizePrintMessage, root.GetSizeFromHere());
        }
Beispiel #3
0
        public static void FillFolderWithFiles(DirectoryInfo directory, Folder folder)
        {
            foreach (FileInfo file in directory.GetFiles())
            {
                folder.Files.Add(new File(file.Name, file.Length));
            }

            foreach (var subDir in directory.GetDirectories())
            {
                var subFolder = new Folder(subDir.Name);
                folder.SubFolders.Add(subFolder);
                FillFolderWithFiles(subDir, subFolder);
            }
        }
        private static void TraverseMyFolderDFS(Folder folder)
        {
            var folders = folder.Folders;
            foreach (var item in folders)
            {
                Console.WriteLine("Folder name: " + item.Name);
                TraverseMyFolderDFS(item);
            }

            var files = folder.Files;
            foreach (var item in files)
            {
                Console.WriteLine("File name: " + item.Name);
            }
        }
Beispiel #5
0
        private static void PrintFromFolder(Folder folder, int offset)
        {
            Console.Write(new string('-', offset) + folder.Name);

            if (offset == 0)
            {
                Console.Write(ShowRootPrintMessage);
            }

            Console.WriteLine();

            foreach (var subfolder in folder.SubFolders)
            {
                PrintFromFolder(subfolder, offset + 2);
            }

            foreach (var file in folder.Files)
            {
                Console.WriteLine(new string('-', offset) + file.Name);
            }
        }