Как сделать исключение при вводе неправильной команды - C#
Формулировка задачи:
using System;
using System.IO;
using System.Diagnostics;
using KIROS;
using System.Threading;
namespace ZIKIRO
{
namespace KIROS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Загрузка OS KIRO...");
Thread.Sleep(3000);
Console.WriteLine("Загрузка OS KIRO...");
Thread.Sleep(3000);
Console.WriteLine("Загрузка OS KIRO...");
Thread.Sleep(1800);//Имитируем загрузку нашей псевдо-OS.
Console.WriteLine("OS KIRO готова к работе");
Console.Write("command>> ");
while (true)
{
var In = Console.ReadLine();
if (In == "help")
{
Console.WriteLine(" help - справка. \n folder - открыть менеджер директорий. \n OFF - выключение PC. \n KDir - открыть проводник основной ОС.");
Console.Write("command>> ");
}
if (In == "OFF")
{
Console.WriteLine("Выключаю PC...");
Console.Write("command>> ");
Process.Start("shutdown.exe", "/s /t 0");
}
{
if (In == "folder")
{
Console.WriteLine("Чтение папок на D:/");
DirectoryInfo[] cDirs = new DirectoryInfo(@"E:\").GetDirectories();
// Записываем полученный список в файл
using (StreamWriter sw = new StreamWriter("DirectoryInfo.txt"))
{
foreach (DirectoryInfo dir in cDirs)
{
sw.WriteLine(dir.Name);
}
}
// Читаем записанные данные
string line = "";
using (StreamReader sr = new StreamReader("DirectoryInfo.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.Write("command>> ");
}
} if (In == "KDir")
{
Console.WriteLine("Открываю проводник");
Process.Start("explorer", "");
Console.Write("command>> ");
} if (In == "total")
{
Console.WriteLine("Открываю проводник");
Process.Start("totalcmd.exe", "");
Console.Write("command>> ");
}
}
}
}
}
}
}Console.WriteLine("Неизвестная команда!");
Console.Write("command>> ");Решение задачи: «Как сделать исключение при вводе неправильной команды»
textual
Листинг программы
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Threading;
using System.Diagnostics;
namespace ZIKIRO
{
class OSKIRO
{
public OSKIRO()
{
Start();
}
void Start()
{
Console.Write("Загрузка OS KIRO 10%");
for (int i = 1; i <= 10; i++)
{
Console.Write("\b\b\b");
Thread.Sleep(200);
Console.Write(i+"0%");
}
Console.WriteLine("\nOS KIRO готова к работе");
}
public void Work()
{
bool work = true;
while (work)
{
Console.Write("\ncommand>> ");
string comm = Console.ReadLine();
switch (comm)
{
case "help": Console.WriteLine(" help - справка. \n folder с/d/e- открыть менеджер директорий диска C, D или E. \n OFF - выключение PC. \n KDir - открыть проводник основной ОС."); ;
break;
case "OFF": Process.Start("shutdown.exe", "/s /t 0");
break;
case "folder c":
case "folder d":
case "folder e":
{
string dir = @"C:\";
if (comm == "folder d") dir = @"D:\";
else if (comm == "folder e") dir = @"E:\";
Console.WriteLine("Спосок папок на " + dir);
foreach (DirectoryInfo item in new DirectoryInfo(dir).GetDirectories())
{
Console.WriteLine(item.Name);
}
}
break;
case "KDir":
{
Console.WriteLine("Открываю проводник");
Process.Start("explorer");
}
break;
case "total":
{
Console.WriteLine("Открываю проводник");
Process.Start("totalcmd.exe");
}
break;
case "exit": work = false;
break;
default: Console.WriteLine("Команда не найдена!");
break;
}
}
Console.WriteLine("OS KIRO завершает свою работу");
}
}
class Program
{
static void Main(string[] args)
{
OSKIRO myOS = new OSKIRO();
myOS.Work();
Console.ReadKey();
}
}
}