public void WriteDirectoryStats(string filePath, DirectoryStats directory)
 {
     getDirectoryStringList(directory);
     try
     {
         File.WriteAllLines(@filePath, directoryStrings.ToArray());
     }
     catch (UnauthorizedAccessException)
     {
         Console.WriteLine("Unauthorized Access to {0}", filePath);
     }
 }
        private DirectoryStats GetDirectories(string path)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path);

            string[]       files, subdirectories = new string[] { };
            DirectoryStats directory             = new DirectoryStats(directoryInfo.Name);

            try
            {
                files          = Directory.GetFiles(path);
                subdirectories = Directory.GetDirectories(path);
            }

            catch (UnauthorizedAccessException)
            {
                directory.access = false;
                return(directory);
            }

            //Adds all the directory's files into a dictionary and includes the size of the total files
            if (files.Length > 0)
            {
                List <object> directoryFiles = getDirectoryFiles(files);
                directory.files = (List <FileStats>)directoryFiles[1];
                directory.size += (long)directoryFiles[0];
            }

            //Recursively checks all subdirectories for the same criteria. Adding them to subdictionaries
            //Sums up the total directory size as it goes.
            if (subdirectories.Length > 0)
            {
                List <DirectoryStats> subdirectoryList = new List <DirectoryStats>();

                foreach (string subPath in subdirectories)
                {
                    DirectoryStats subdirectory = GetDirectories(subPath);
                    directory.size += subdirectory.size;
                    subdirectoryList.Add(subdirectory);
                }

                directory.subdirectories = subdirectoryList;
            }

            return(directory);
        }
        //Recursively adds strings to directoryStrings with the correct indent
        private void getDirectoryStringList(DirectoryStats dir, int initialIndent = 0)
        {
            directoryStrings.Add(buildDirectoryString(dir.name, initialIndent, dir.size));

            if (dir.subdirectories.Count != 0)
            {
                foreach (DirectoryStats subdirectory in dir.subdirectories)
                {
                    getDirectoryStringList(subdirectory, initialIndent + indentStep);
                }
            }

            if (dir.files.Count != 0)
            {
                foreach (FileStats file in dir.files)
                {
                    directoryStrings.Add(buildDirectoryString(file.name, initialIndent + indentStep, file.size));
                }
            }
        }
        //Sorts by size in Descending order
        public void SortSubdirectories(DirectoryStats dir)
        {
            //Recursively sets subdirectories to sort themselves
            foreach (DirectoryStats subdirectory in dir.subdirectories)
            {
                SortSubdirectories(subdirectory);
            }

            //Once all subdirectories have been sorted the directory sorts itself
            if (dir.subdirectories.Count != 0)
            {
                dir.subdirectories = dir.subdirectories.OrderByDescending(x => x.size).ToList();
            }

            //Sorts the directories files (if there are any)
            if (dir.files.Count != 0)
            {
                dir.files = dir.files.OrderByDescending(x => x.size).ToList();
            }
        }
        static void Main(string[] args)
        {
            DirectorySearcher searcher = new DirectorySearcher();
            //Introduce and get input for file system
            //Also get input for output path
            string fileSystem      = "";
            bool   directoryExists = false;
            string outputPath      = "";

            while (!directoryExists)
            {
                Console.WriteLine("Type a Directory to search through (default is C:/)");
                fileSystem = @Console.ReadLine();

                if (fileSystem == "")
                {
                    fileSystem = "C:/";
                }
                DirectoryInfo dirInfo = new DirectoryInfo(fileSystem);
                if (dirInfo.Exists)
                {
                    directoryExists = true;
                }
                else
                {
                    Console.WriteLine("Directory Not Found please try again");
                }
            }

            Console.WriteLine("Enter Output Path for .txt file");
            Console.WriteLine("Adding only filename will add .txt file to {0}", fileSystem);
            outputPath = Console.ReadLine();

            if (!outputPath.Contains("/") || !outputPath.Contains("\\") || !outputPath.Contains("//") || !outputPath.Contains(@"\"))
            {
                string fileName = outputPath;
                if (fileSystem != "C:/") //C:/ Will prevent writing so adding this check
                {
                    outputPath = fileSystem + @"/" + fileName;
                }
                else
                {
                    outputPath = fileSystem + @"/Users/Public/Documents/" + fileName;
                }
            }

            if (outputPath == "")
            {
                if (fileSystem != "C:/") //C:/ Will prevent writing so adding this check
                {
                    outputPath = fileSystem + "text.txt";
                }
                else
                {
                    outputPath = fileSystem + @"/Users/" + "text.txt";
                }
            }

            outputPath = checkFileExtension(outputPath);

            Console.WriteLine("Reading Directories...");
            DirectoryStats directories = searcher.GetFullDirectory(directoryPath: fileSystem);

            Console.WriteLine("Sorting");
            directories.SortSubdirectories(directories);
            TextWriter writer = new TextWriter();

            Console.WriteLine("Writing to {0}", outputPath);
            writer.WriteDirectoryStats(outputPath, directories);
            Console.WriteLine("Complete");

            Console.ReadLine();
        }