Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sing System.Text;
- using System;
- using System.IO;
- namespace Menu
- {
- class Program
- {
- delegate void method();
- static void Main(string[] args)
- {
- Menu();
- static void Menu()
- {
- string[] items = { "Создание файла", "Чтение из файла", "Сохранение в файл", "Сохранение в новый файл", "Переименование файла", "Удаление файла", "Выход" };
- method[] methods = new method[] { Method1, Method2, Method3, Method4, Method5, Method6, Exit };
- ConsoleMenu menu = new ConsoleMenu(items);
- int menuResult;
- do
- {
- menuResult = menu.PrintMenu();
- methods[menuResult]();
- Console.WriteLine("Для продолжения нажмите любую клавишу");
- Console.ReadKey();
- } while (menuResult != items.Length - 1);
- }
- static void Method1()
- {
- Console.WriteLine("Выбрано действие 'Создание файла' ");
- Console.WriteLine("Укажите путь к папке где хотите создать файл");
- try
- {
- string path = Console.ReadLine();
- File.Create(path);
- }
- catch (Exception)
- {
- Console.WriteLine("Ошибка!");
- }
- }
- static void Method2()
- {
- Console.WriteLine("Выбрано действие 'Чтение из файла' ");
- Console.WriteLine("Укажите путь к файлу котрый хотите открыть для чтения ");
- try
- {
- string path = Console.ReadLine();
- FileStream file1 = new FileStream(path, FileMode.Open);
- StreamReader reader = new StreamReader(file1);
- Console.WriteLine($"Текст из файла: {reader.ReadToEnd()}");
- reader.Close();
- }
- catch (Exception)
- {
- Console.WriteLine("Ошибка!");
- }
- }
- static void Method3()
- {
- Console.WriteLine("Выбрано действие 'Сохранение в файл' ");
- Console.WriteLine("Введите текст который хотите дозаписать");
- string text = Console.ReadLine();
- Console.WriteLine("Укажите путь к файлу который хотите дозаписать ");
- try
- {
- string path = Console.ReadLine();
- File.AppendAllText(path, text);
- }
- catch (Exception)
- {
- Console.WriteLine("Ошибка!");
- }
- }
- static void Method4()
- {
- Console.WriteLine("Выбрано действие 'Сохранение в новый файл' ");
- Console.WriteLine("Введите текст который хотите перезаписать");
- string text = Console.ReadLine();
- Console.WriteLine("Укажите путь к файлу котрый хотите перезаписать ");
- try
- {
- string path = Console.ReadLine();
- }
- catch (Exception)
- {
- Console.WriteLine("Ошибка!");
- }
- }
- static void Method5()
- {
- Console.WriteLine("Выбрано действие 'Переименование файла' ");
- }
- static void Method6()
- {
- Console.WriteLine("Выбрано действие 'Удаление файла' ");
- Console.WriteLine("Укажите путь к файлу котрый хотите удалить ");
- try
- {
- string path = Console.ReadLine();
- File.Delete(path);
- }
- catch (Exception)
- {
- Console.WriteLine("Ошибка!");
- }
- }
- static void Exit()
- {
- Console.WriteLine("Приложение заканчивает работу!");
- }
- }
- class ConsoleMenu
- {
- string[] menuItems;
- int counter = 0;
- public ConsoleMenu(string[] menuItems)
- {
- this.menuItems = menuItems;
- }
- public int PrintMenu()
- {
- ConsoleKeyInfo key;
- do
- {
- Console.Clear();
- for (int i = 0; i < menuItems.Length; i++)
- {
- if (counter == i)
- {
- Console.BackgroundColor = ConsoleColor.Cyan;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WriteLine(menuItems[i]);
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- Console.WriteLine(menuItems[i]);
- }
- key = Console.ReadKey();
- if (key.Key == ConsoleKey.UpArrow)
- {
- counter--;
- if (counter == -1) counter = menuItems.Length - 1;
- }
- if (key.Key == ConsoleKey.DownArrow)
- {
- counter++;
- if (counter == menuItems.Length) counter = 0;
- }
- }
- while (key.Key != ConsoleKey.Enter);
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement