Advertisement
desislava_topuzakova

Untitled

May 28th, 2022
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. namespace DirectoryTraversal
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7.  
  8. public class DirectoryTraversal
  9. {
  10. static void Main()
  11. {
  12. string path = Console.ReadLine();
  13. string reportFileName = @"\report.txt";
  14.  
  15. string reportContent = TraverseDirectory(path);
  16. Console.WriteLine(reportContent);
  17.  
  18. WriteReportToDesktop(reportContent, reportFileName);
  19. }
  20.  
  21. public static string TraverseDirectory(string inputFolderPath)
  22. {
  23. string[] files = Directory.GetFiles(inputFolderPath);
  24. //разширение -> списък с файловете
  25. Dictionary<string, List<FileInfo>> extensionsInfo = new Dictionary<string, List<FileInfo>>();
  26. //за всеки файл ни трябва разширението
  27. foreach(string file in files)
  28. {
  29. //информация за файла -> разришението
  30. FileInfo fileInfo = new FileInfo(file);
  31. string extension = fileInfo.Extension;
  32.  
  33. if (!extensionsInfo.ContainsKey(extension))
  34. {
  35. extensionsInfo.Add(extension, new List<FileInfo>());
  36. }
  37.  
  38. extensionsInfo[extension].Add(fileInfo);
  39. }
  40.  
  41.  
  42. //1. be ordered by the count of list descending
  43. //2. name extenstions ascending
  44. //запис: key(разширение) -> value (списък с файлове)
  45.  
  46. foreach (var entry in extensionsInfo.OrderByDescending(entry => entry.Value.Count).ThenBy(entry => entry.Key))
  47. {
  48. //вземем на всяко разширение списъка с файловете
  49.  
  50. string extension = entry.Key;
  51. Console.WriteLine(extension); //TODO: put in stringBuilder
  52. List<FileInfo> filesInfo = entry.Value;
  53. //списък с файловете трябва да се сортира спрямо размета на файла
  54. filesInfo.OrderByDescending(file => file.Length);
  55.  
  56. foreach (FileInfo fileInfo in filesInfo)
  57. {
  58. //BYTES / 1024 -> KB
  59. Console.WriteLine($"--{fileInfo.Name} - {fileInfo.Length / 1024:F3}kb"); //TODO: put in stringBuilder
  60. }
  61. }
  62.  
  63. return ""; //TODO: return sb.toString();
  64. }
  65.  
  66. public static void WriteReportToDesktop(string textContent, string reportFileName)
  67. {
  68. //textContent да го напиша във файл с име reportFileName
  69. string pathReport = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + reportFileName;
  70. //"C:\Users\I353529\Desktop" + "\report.txt" -> "C:\Users\I353529\Desktop\report.txt"
  71. File.WriteAllText(pathReport, textContent);
  72. }
  73. }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement