|
@@ -0,0 +1,125 @@
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using System.IO;
|
|
|
|
+
|
|
|
|
+namespace AlekseervMDK05._04Lab4
|
|
|
|
+{
|
|
|
|
+ class Program
|
|
|
|
+ {
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Головной модуль,в котором происходит перебор всех файлов, которые были отсортированы
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="args"></param>
|
|
|
|
+ static void Main(string[] args)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine("Введите путь к директории:");
|
|
|
|
+ string sourcePath = Console.ReadLine();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!Directory.Exists(sourcePath))
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine("Указанная директория не существует.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ long freeSpace = GetFreeSpace(sourcePath);
|
|
|
|
+ Console.WriteLine($"Свободное место: {freeSpace / (1024 * 1024)} MB");
|
|
|
|
+
|
|
|
|
+ string imagesPath = Path.Combine(sourcePath, "Images");
|
|
|
|
+ string documentsPath = Path.Combine(sourcePath, "Documents");
|
|
|
|
+ string othersPath = Path.Combine(sourcePath, "Others");
|
|
|
|
+
|
|
|
|
+ Directory.CreateDirectory(imagesPath);
|
|
|
|
+ Directory.CreateDirectory(documentsPath);
|
|
|
|
+ Directory.CreateDirectory(othersPath);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ var files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
|
|
|
|
+ int imageCount = 0;
|
|
|
|
+ int documentCount = 0;
|
|
|
|
+ int otherCount = 0;
|
|
|
|
+
|
|
|
|
+ foreach (var file in files)
|
|
|
|
+ {
|
|
|
|
+ string extension = Path.GetExtension(file).ToLower();
|
|
|
|
+
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (IsImage(extension))
|
|
|
|
+ {
|
|
|
|
+ MoveFile(file, imagesPath);
|
|
|
|
+ imageCount++;
|
|
|
|
+ }
|
|
|
|
+ else if (IsDocument(extension))
|
|
|
|
+ {
|
|
|
|
+ MoveFile(file, documentsPath);
|
|
|
|
+ documentCount++;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ MoveFile(file, othersPath);
|
|
|
|
+ otherCount++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine($"Ошибка при перемещении файла {file}: {ex.Message}");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ string infoPath = Path.Combine(sourcePath, "Info.txt");
|
|
|
|
+ File.WriteAllText(infoPath, $"Изображения: {imageCount}\nДокументы: {documentCount}\nПрочие: {otherCount}");
|
|
|
|
+
|
|
|
|
+ Console.WriteLine("Сортировка завершена. Информация записана в Info.txt.");
|
|
|
|
+ Console.ReadLine();
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Получаем информацию о свободном пространстве накопителя
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="path"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ static long GetFreeSpace(string path)
|
|
|
|
+ {
|
|
|
|
+ DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(path));
|
|
|
|
+ return driveInfo.AvailableFreeSpace;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Поиск картинок
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="extension"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ static bool IsImage(string extension)
|
|
|
|
+ {
|
|
|
|
+ string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff" };
|
|
|
|
+ return imageExtensions.Contains(extension);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Поиск документов
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="extension"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ static bool IsDocument(string extension)
|
|
|
|
+ {
|
|
|
|
+ string[] documentExtensions = { ".pdf", ".doc", ".docx", ".txt", ".xls", ".xlsx" };
|
|
|
|
+ return documentExtensions.Contains(extension);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Перемещение файла
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="sourceFile"></param>
|
|
|
|
+ /// <param name="destFolder"></param>
|
|
|
|
+ static void MoveFile(string sourceFile, string destFolder)
|
|
|
|
+ {
|
|
|
|
+ string fileName = Path.GetFileName(sourceFile);
|
|
|
|
+ string destFile = Path.Combine(destFolder, fileName);
|
|
|
|
+ File.Move(sourceFile, destFile);
|
|
|
|
+ Console.WriteLine($"Файл {fileName} перемещён в {destFolder}");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|