Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. using 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. string[] it = { "Авторизация", "Регистрация" };
  13. method[] method = new method[] { avt1, avt2 };
  14. ConsoleMenu menu2 = new ConsoleMenu(it);
  15. int Result;
  16. do
  17. {
  18. Result = menu2.PrintMenu();
  19. method[Result]();
  20. } while (Result != it.Length - 1);
  21.  
  22. void avt2()
  23. {
  24. g1:
  25. try
  26. {
  27.  
  28.  
  29. bool avtor = false;
  30. do
  31. {
  32. Console.Write("Введите Логин:");
  33. string log = Console.ReadLine();
  34. Console.Write("Введите Пароль:");
  35. string pass = Console.ReadLine();
  36. Console.Write("Введите пароль повторно:");
  37. string pass2 = Console.ReadLine();
  38. if (File.Exists($@"C:\Menu\{log}.txt"))
  39. {
  40. Console.WriteLine("Данный пользователь уже создан, создайте нового!");
  41.  
  42. }
  43. else
  44. {
  45. if (pass == pass2)
  46. {
  47. Console.WriteLine("Вы зарегистрировали новый аккаунт");
  48. File.AppendAllText($@"C:\Menu\{log}.txt", $"{pass}");
  49. avtor = true;
  50. }
  51. else
  52. {
  53. Console.WriteLine("Ошибка,пароли не совпадают!");
  54. avtor = false;
  55. }
  56. }
  57. }
  58. while (avtor == false);
  59. }
  60. catch (SystemException)
  61. {
  62. Console.WriteLine("Что то пошло не так, повторите попытку");
  63. goto g1;
  64. }
  65. }
  66. void avt1()
  67. {
  68. g2:
  69. try
  70. {
  71. bool avtor = false;
  72. do
  73. {
  74. Console.Write("Введите Логин:");
  75. string log = Console.ReadLine();
  76. if (File.Exists($@"C:\Menu\{log}.txt"))
  77. {
  78. Console.Write("Введите Пароль:");
  79. string pass = Console.ReadLine();
  80. FileStream fs1;
  81. fs1 = new FileStream($@"C:\Menu\{log}.txt", FileMode.Open);
  82. if (fs1.Length == pass.Length)
  83. {
  84. avtor = true;
  85. Menu();
  86. }
  87. }
  88. else
  89. {
  90. Console.WriteLine("Ошибка");
  91. avtor = false;
  92.  
  93. }
  94. }
  95. while (avtor == false);
  96. }
  97. catch (SystemException)
  98. {
  99. Console.WriteLine("Что то пошло не так, повторите попытку");
  100. goto g2;
  101. }
  102. }
  103.  
  104.  
  105. }
  106. static void Menu()
  107. {
  108. string[] items = { "Создание файла", "Чтение из файла", "Сохранение в файл", "Сохранение в новый файл", "Переименование файла", "Удаление файла", "Выход" };
  109. method[] methods = new method[] { Method1, Method2, Method3, Method4, Method5, Method6, Exit };
  110. ConsoleMenu menu = new ConsoleMenu(items);
  111. int menuResult;
  112. do
  113. {
  114. menuResult = menu.PrintMenu();
  115. methods[menuResult]();
  116. Console.WriteLine("Для продолжения нажмите любую клавишу");
  117. Console.ReadKey();
  118. } while (menuResult != items.Length - 1);
  119. }
  120. static void Method1()
  121. {
  122. Console.WriteLine("Выбрано действие 'Создание файла' ");
  123.  
  124. Console.WriteLine("Укажите путь к папке где хотите создать файл");
  125. try
  126. {
  127. string path = Console.ReadLine();
  128. File.Create(path);
  129. }
  130. catch (Exception)
  131. {
  132. Console.WriteLine("Ошибка!");
  133. }
  134.  
  135. }
  136. static void Method2()
  137. {
  138. Console.WriteLine("Выбрано действие 'Чтение из файла' ");
  139.  
  140. Console.WriteLine("Укажите путь к файлу котрый хотите открыть для чтения ");
  141. try
  142. {
  143. string path = Console.ReadLine();
  144. FileStream file1 = new FileStream(path, FileMode.Open);
  145. StreamReader reader = new StreamReader(file1);
  146. Console.WriteLine($"Текст из файла: {reader.ReadToEnd()}");
  147. reader.Close();
  148. }
  149. catch (Exception)
  150. {
  151. Console.WriteLine("Ошибка!");
  152. }
  153.  
  154. }
  155. static void Method3()
  156. {
  157. Console.WriteLine("Выбрано действие 'Сохранение в файл' ");
  158.  
  159. Console.WriteLine("Введите текст который хотите дозаписать");
  160. string text = Console.ReadLine();
  161. Console.WriteLine("Укажите путь к файлу который хотите дозаписать ");
  162. try
  163. {
  164. string path = Console.ReadLine();
  165. File.AppendAllText(path, text);
  166. }
  167. catch (Exception)
  168. {
  169. Console.WriteLine("Ошибка!");
  170. }
  171. }
  172. static void Method4()
  173. {
  174. Console.WriteLine("Выбрано действие 'Сохранение в новый файл' ");
  175.  
  176. Console.WriteLine("Введите текст который хотите перезаписать");
  177. string text = Console.ReadLine();
  178. Console.WriteLine("Укажите путь к файлу котрый хотите перезаписать ");
  179. try
  180. {
  181. string path = Console.ReadLine();
  182.  
  183. }
  184. catch (Exception)
  185. {
  186. Console.WriteLine("Ошибка!");
  187. }
  188. }
  189. static void Method5()
  190. {
  191. Console.WriteLine("Выбрано действие 'Переименование файла' ");
  192. }
  193. static void Method6()
  194. {
  195. Console.WriteLine("Выбрано действие 'Удаление файла' ");
  196.  
  197. Console.WriteLine("Укажите путь к файлу котрый хотите удалить ");
  198. try
  199. {
  200. string path = Console.ReadLine();
  201. File.Delete(path);
  202. }
  203. catch (Exception)
  204. {
  205. Console.WriteLine("Ошибка!");
  206. }
  207.  
  208. }
  209. static void Exit()
  210. {
  211. Console.WriteLine("Приложение заканчивает работу!");
  212. }
  213. }
  214. class ConsoleMenu
  215. {
  216. string[] menuItems;
  217. int counter = 0;
  218. public ConsoleMenu(string[] menuItems)
  219. {
  220. this.menuItems = menuItems;
  221. }
  222.  
  223. public int PrintMenu()
  224. {
  225. ConsoleKeyInfo key;
  226. do
  227. {
  228. Console.Clear();
  229. for (int i = 0; i < menuItems.Length; i++)
  230. {
  231. if (counter == i)
  232. {
  233. Console.BackgroundColor = ConsoleColor.Cyan;
  234. Console.ForegroundColor = ConsoleColor.Black;
  235. Console.WriteLine(menuItems[i]);
  236. Console.BackgroundColor = ConsoleColor.Black;
  237. Console.ForegroundColor = ConsoleColor.White;
  238. }
  239. else
  240. Console.WriteLine(menuItems[i]);
  241.  
  242. }
  243. key = Console.ReadKey();
  244. if (key.Key == ConsoleKey.UpArrow)
  245. {
  246. counter--;
  247. if (counter == -1) counter = menuItems.Length - 1;
  248. }
  249. if (key.Key == ConsoleKey.DownArrow)
  250. {
  251. counter++;
  252. if (counter == menuItems.Length) counter = 0;
  253. }
  254. }
  255. while (key.Key != ConsoleKey.Enter);
  256. return counter;
  257. }
  258.  
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement