Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Drawing;
- namespace Task1
- {
- class Program
- {
- /// <summary>
- /// Метод производит ввод данных (числа) и проверку на соответствие условиям
- /// <returns>В случае успешного ввода возвращает натуральное число, иначе -1 </returns>
- static int ReadInt()
- {
- int x;
- if (!int.TryParse(Console.ReadLine(), out x) || x < 1)
- {
- x = -1;
- }
- return x;
- }
- static void DirectoryOverview(string path, int K, int currentLevel)
- {
- if (currentLevel > K) return;
- //создаем объект типа directoryInfo, хранящий информацию о директории по пути
- var directoryInfo = new DirectoryInfo(path);
- //выводим информацию о соответствующих свойствах директории
- Console.WriteLine($"{path}\n" +
- //список всех атрибутов директории
- $"attributes: {directoryInfo.Attributes}; " +
- //время создания
- $"creation time: {directoryInfo.CreationTime} " +
- //последнее обновление
- $"last update:{directoryInfo.LastWriteTime}\n");
- var directories = Directory.GetDirectories(path);
- for (int i = 0; i < directories.Length; i++)
- {
- var directory = directories[i];
- //рекурсивно выполняем метод для всех вложенных директорий
- DirectoryOverview(directory, K, currentLevel+1);
- }
- }
- static void Main()
- {
- do
- {
- ConsoleKey key;
- string path;
- do
- {
- Console.Clear();
- Console.WriteLine("Нажмите 1, для ввода пути директории");
- Console.WriteLine("Нажмите 2, для выбора папки из диалогового окна");
- key = Console.ReadKey().Key;
- if (key == ConsoleKey.D1 || key == ConsoleKey.D2)
- {
- break;
- }
- } while (true);
- Console.WriteLine("\nВведите уровень вложенности");
- int K;
- while ((K = ReadInt()) == -1)
- {
- Console.WriteLine("Введите целое положительное число");
- }
- if (key == ConsoleKey.D1)
- {
- Console.WriteLine("Введите путь директории (относительный или абсолютный)");
- Console.WriteLine("При вводе \"current\" будет выбрана текущая папка");
- path = Console.ReadLine();
- try
- {
- if (path == "current")
- path = Directory.GetCurrentDirectory();
- //вывести абсолютный путь
- DirectoryOverview(path, K, 1);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- else
- {
- /*
- FolderBrowserDialog FBD = new FolderBrowserDialog();
- if (FBD.ShowDialog() == DialogResult.OK)
- {
- MessageBox.Show(FBD.SelectedPath);
- }*/
- }
- Console.WriteLine("Нажмите Enter, чтобы продолжить\nНажмите Escape для выхода");
- }
- while (Console.ReadKey().Key != ConsoleKey.Escape);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement