Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4.  
  5. namespace Task1
  6. {
  7.     class Program
  8.     {
  9.         /// <summary>
  10.         /// Метод производит ввод данных (числа) и проверку на соответствие условиям
  11.         /// <returns>В случае успешного ввода возвращает натуральное число, иначе -1 </returns>
  12.         static int ReadInt()
  13.         {
  14.             int x;
  15.             if (!int.TryParse(Console.ReadLine(), out x) || x < 1)
  16.             {
  17.                 x = -1;
  18.             }
  19.             return x;
  20.         }
  21.         static void DirectoryOverview(string path, int K, int currentLevel)
  22.         {
  23.             if (currentLevel > K) return;
  24.  
  25.             //создаем объект типа directoryInfo, хранящий информацию о директории по пути
  26.             var directoryInfo = new DirectoryInfo(path);
  27.  
  28.             //выводим информацию о соответствующих свойствах директории
  29.             Console.WriteLine($"{path}\n" +
  30.             //список всех атрибутов директории
  31.             $"attributes: {directoryInfo.Attributes}; " +
  32.             //время создания
  33.             $"creation time: {directoryInfo.CreationTime} " +
  34.             //последнее обновление
  35.             $"last update:{directoryInfo.LastWriteTime}\n");
  36.  
  37.             var directories = Directory.GetDirectories(path);
  38.  
  39.             for (int i = 0; i < directories.Length; i++)
  40.             {
  41.                 var directory = directories[i];
  42.                
  43.                 //рекурсивно выполняем метод для всех вложенных директорий
  44.                 DirectoryOverview(directory, K, currentLevel+1);
  45.             }
  46.         }
  47.         static void Main()
  48.         {
  49.             do
  50.             {
  51.                 ConsoleKey key;
  52.                 string path;
  53.                 do
  54.                 {
  55.                     Console.Clear();
  56.  
  57.                     Console.WriteLine("Нажмите 1, для ввода пути директории");
  58.                     Console.WriteLine("Нажмите 2, для выбора папки из диалогового окна");
  59.  
  60.                     key = Console.ReadKey().Key;
  61.                     if (key == ConsoleKey.D1 || key == ConsoleKey.D2)
  62.                     {
  63.                         break;
  64.                     }
  65.  
  66.                 } while (true);
  67.  
  68.                 Console.WriteLine("\nВведите уровень вложенности");
  69.                 int K;
  70.  
  71.                 while ((K = ReadInt()) == -1)
  72.                 {
  73.                     Console.WriteLine("Введите целое положительное число");
  74.                 }
  75.  
  76.                 if (key == ConsoleKey.D1)
  77.                 {
  78.                     Console.WriteLine("Введите путь директории (относительный или абсолютный)");
  79.                     Console.WriteLine("При вводе \"current\" будет выбрана текущая папка");
  80.                     path = Console.ReadLine();
  81.                     try
  82.                     {
  83.                         if (path == "current")
  84.                             path = Directory.GetCurrentDirectory();
  85.  
  86.                         //вывести абсолютный путь
  87.                         DirectoryOverview(path, K, 1);
  88.                     }
  89.                     catch (Exception ex)
  90.                     {
  91.                         Console.WriteLine(ex.Message);
  92.                     }
  93.  
  94.                 }
  95.                 else
  96.                 {
  97.                     /*
  98.                     FolderBrowserDialog FBD = new FolderBrowserDialog();
  99.                     if (FBD.ShowDialog() == DialogResult.OK)
  100.                     {
  101.                         MessageBox.Show(FBD.SelectedPath);
  102.                     }*/
  103.                 }
  104.  
  105.                 Console.WriteLine("Нажмите Enter, чтобы продолжить\nНажмите Escape для выхода");
  106.             }
  107.             while (Console.ReadKey().Key != ConsoleKey.Escape);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement