Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace AlekseervMDK05._04Lab4
  8. {
  9. class Program
  10. {
  11. /// <summary>
  12. /// Головной модуль,в котором происходит перебор всех файлов, которые были отсортированы
  13. /// </summary>
  14. /// <param name="args"></param>
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine("Введите путь к директории:");
  18. string sourcePath = Console.ReadLine();
  19. if (!Directory.Exists(sourcePath))
  20. {
  21. Console.WriteLine("Указанная директория не существует.");
  22. return;
  23. }
  24. long freeSpace = GetFreeSpace(sourcePath);
  25. Console.WriteLine($"Свободное место: {freeSpace / (1024 * 1024)} MB");
  26. string imagesPath = Path.Combine(sourcePath, "Images");
  27. string documentsPath = Path.Combine(sourcePath, "Documents");
  28. string othersPath = Path.Combine(sourcePath, "Others");
  29. Directory.CreateDirectory(imagesPath);
  30. Directory.CreateDirectory(documentsPath);
  31. Directory.CreateDirectory(othersPath);
  32. var files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
  33. int imageCount = 0;
  34. int documentCount = 0;
  35. int otherCount = 0;
  36. foreach (var file in files)
  37. {
  38. string extension = Path.GetExtension(file).ToLower();
  39. try
  40. {
  41. if (IsImage(extension))
  42. {
  43. MoveFile(file, imagesPath);
  44. imageCount++;
  45. }
  46. else if (IsDocument(extension))
  47. {
  48. MoveFile(file, documentsPath);
  49. documentCount++;
  50. }
  51. else
  52. {
  53. MoveFile(file, othersPath);
  54. otherCount++;
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. Console.WriteLine($"Ошибка при перемещении файла {file}: {ex.Message}");
  60. }
  61. }
  62. string infoPath = Path.Combine(sourcePath, "Info.txt");
  63. File.WriteAllText(infoPath, $"Изображения: {imageCount}\nДокументы: {documentCount}\nПрочие: {otherCount}");
  64. Console.WriteLine("Сортировка завершена. Информация записана в Info.txt.");
  65. Console.ReadLine();
  66. }
  67. /// <summary>
  68. /// Получаем информацию о свободном пространстве накопителя
  69. /// </summary>
  70. /// <param name="path"></param>
  71. /// <returns></returns>
  72. static long GetFreeSpace(string path)
  73. {
  74. DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(path));
  75. return driveInfo.AvailableFreeSpace;
  76. }
  77. /// <summary>
  78. /// Поиск картинок
  79. /// </summary>
  80. /// <param name="extension"></param>
  81. /// <returns></returns>
  82. static bool IsImage(string extension)
  83. {
  84. string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff" };
  85. return imageExtensions.Contains(extension);
  86. }
  87. /// <summary>
  88. /// Поиск документов
  89. /// </summary>
  90. /// <param name="extension"></param>
  91. /// <returns></returns>
  92. static bool IsDocument(string extension)
  93. {
  94. string[] documentExtensions = { ".pdf", ".doc", ".docx", ".txt", ".xls", ".xlsx" };
  95. return documentExtensions.Contains(extension);
  96. }
  97. /// <summary>
  98. /// Перемещение файла
  99. /// </summary>
  100. /// <param name="sourceFile"></param>
  101. /// <param name="destFolder"></param>
  102. static void MoveFile(string sourceFile, string destFolder)
  103. {
  104. string fileName = Path.GetFileName(sourceFile);
  105. string destFile = Path.Combine(destFolder, fileName);
  106. File.Move(sourceFile, destFile);
  107. Console.WriteLine($"Файл {fileName} перемещён в {destFolder}");
  108. }
  109. }
  110. }