Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. sing System.Text;
  2. using System;
  3. using System.IO;
  4.  
  5. namespace Menu
  6. {
  7. class Program
  8. {
  9. delegate void method();
  10. static void Main(string[] args)
  11. {
  12. Menu();
  13. static void Menu()
  14. {
  15. string[] items = { "Создание файла", "Чтение из файла", "Сохранение в файл", "Сохранение в новый файл", "Переименование файла", "Удаление файла", "Выход" };
  16. method[] methods = new method[] { Method1, Method2, Method3, Method4, Method5, Method6, Exit };
  17. ConsoleMenu menu = new ConsoleMenu(items);
  18. int menuResult;
  19. do
  20. {
  21. menuResult = menu.PrintMenu();
  22. methods[menuResult]();
  23. Console.WriteLine("Для продолжения нажмите любую клавишу");
  24. Console.ReadKey();
  25. } while (menuResult != items.Length - 1);
  26. }
  27. static void Method1()
  28. {
  29. Console.WriteLine("Выбрано действие 'Создание файла' ");
  30.  
  31. Console.WriteLine("Укажите путь к папке где хотите создать файл");
  32. try
  33. {
  34. string path = Console.ReadLine();
  35. File.Create(path);
  36. }
  37. catch (Exception)
  38. {
  39. Console.WriteLine("Ошибка!");
  40. }
  41.  
  42. }
  43. static void Method2()
  44. {
  45. Console.WriteLine("Выбрано действие 'Чтение из файла' ");
  46.  
  47. Console.WriteLine("Укажите путь к файлу котрый хотите открыть для чтения ");
  48. try
  49. {
  50. string path = Console.ReadLine();
  51. FileStream file1 = new FileStream(path, FileMode.Open);
  52. StreamReader reader = new StreamReader(file1);
  53. Console.WriteLine($"Текст из файла: {reader.ReadToEnd()}");
  54. reader.Close();
  55. }
  56. catch (Exception)
  57. {
  58. Console.WriteLine("Ошибка!");
  59. }
  60.  
  61. }
  62. static void Method3()
  63. {
  64. Console.WriteLine("Выбрано действие 'Сохранение в файл' ");
  65.  
  66. Console.WriteLine("Введите текст который хотите дозаписать");
  67. string text = Console.ReadLine();
  68. Console.WriteLine("Укажите путь к файлу который хотите дозаписать ");
  69. try
  70. {
  71. string path = Console.ReadLine();
  72. File.AppendAllText(path, text);
  73. }
  74. catch (Exception)
  75. {
  76. Console.WriteLine("Ошибка!");
  77. }
  78. }
  79. static void Method4()
  80. {
  81. Console.WriteLine("Выбрано действие 'Сохранение в новый файл' ");
  82.  
  83. Console.WriteLine("Введите текст который хотите перезаписать");
  84. string text = Console.ReadLine();
  85. Console.WriteLine("Укажите путь к файлу котрый хотите перезаписать ");
  86. try
  87. {
  88. string path = Console.ReadLine();
  89.  
  90. }
  91. catch (Exception)
  92. {
  93. Console.WriteLine("Ошибка!");
  94. }
  95. }
  96. static void Method5()
  97. {
  98. Console.WriteLine("Выбрано действие 'Переименование файла' ");
  99. }
  100. static void Method6()
  101. {
  102. Console.WriteLine("Выбрано действие 'Удаление файла' ");
  103.  
  104. Console.WriteLine("Укажите путь к файлу котрый хотите удалить ");
  105. try
  106. {
  107. string path = Console.ReadLine();
  108. File.Delete(path);
  109. }
  110. catch (Exception)
  111. {
  112. Console.WriteLine("Ошибка!");
  113. }
  114.  
  115. }
  116. static void Exit()
  117. {
  118. Console.WriteLine("Приложение заканчивает работу!");
  119. }
  120. }
  121. class ConsoleMenu
  122. {
  123. string[] menuItems;
  124. int counter = 0;
  125. public ConsoleMenu(string[] menuItems)
  126. {
  127. this.menuItems = menuItems;
  128. }
  129.  
  130. public int PrintMenu()
  131. {
  132. ConsoleKeyInfo key;
  133. do
  134. {
  135. Console.Clear();
  136. for (int i = 0; i < menuItems.Length; i++)
  137. {
  138. if (counter == i)
  139. {
  140. Console.BackgroundColor = ConsoleColor.Cyan;
  141. Console.ForegroundColor = ConsoleColor.Black;
  142. Console.WriteLine(menuItems[i]);
  143. Console.BackgroundColor = ConsoleColor.Black;
  144. Console.ForegroundColor = ConsoleColor.White;
  145. }
  146. else
  147. Console.WriteLine(menuItems[i]);
  148.  
  149. }
  150. key = Console.ReadKey();
  151. if (key.Key == ConsoleKey.UpArrow)
  152. {
  153. counter--;
  154. if (counter == -1) counter = menuItems.Length - 1;
  155. }
  156. if (key.Key == ConsoleKey.DownArrow)
  157. {
  158. counter++;
  159. if (counter == menuItems.Length) counter = 0;
  160. }
  161. }
  162. while (key.Key != ConsoleKey.Enter);
  163. return counter;
  164. }
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement