DirectoryTraversal


SUBMITTED BY: bitcoinminers

DATE: Jan. 5, 2016, 7:14 p.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 197303

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace FullDirectoryTraverse
  6. {
  7. class FullDirTraverse
  8. {
  9. const string directory = @"../../";
  10. static string outputdir = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  11. static void WriteFiles(string dir) // write all files in a given directory
  12. {
  13. List<FileInfo> allFiles = new DirectoryInfo(dir).GetFiles().ToList();
  14. var sortedFiles = allFiles.OrderBy(file => file.Length).GroupBy(file => file.Extension).OrderByDescending(group => group.Count()).OrderBy(group => group.Key);
  15. using (var resultWriter = new StreamWriter(outputdir + @"/log.txt", true))
  16. {
  17. foreach(var group in sortedFiles)
  18. {
  19. resultWriter.WriteLine(group.Key);
  20. foreach (var pair in group)
  21. {
  22. resultWriter.WriteLine("name: {0} -- size: {1} bytes ", pair.Name, pair.Length);
  23. }
  24. }
  25. }
  26. }
  27. static void DirectorySearch(string dirPath) // recursion
  28. {
  29. WriteFiles(dirPath); // first write all file in a given Directory
  30. try
  31. {
  32. foreach(string dir in Directory.GetDirectories(dirPath)) // get all directories from the folder
  33. {
  34. WriteFiles(dir);
  35. DirectorySearch(dir); // try the recursion for each of them.
  36. }
  37. }
  38. catch(SystemException exception)
  39. {
  40. Console.WriteLine(exception.Message);
  41. }
  42. }
  43. static void Main()
  44. {
  45. DirectorySearch(directory);
  46. }
  47. }
  48. }

comments powered by Disqus