This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2023_Stanislav_Mykhailenko/Lesson_5/Task_1/Program.cs

97 lines
3 KiB
C#
Raw Normal View History

2023-04-06 17:30:12 +00:00
/*
* Lesson 5 Task 1: a file manager
* Author: Stanislav Mykhailenko, parts of code used from DanyilMykytenko's repository
* License: Unlicense
*/
2023-04-09 20:58:13 +00:00
using Lesson5.Classes;
using Lesson5.Interfaces;
2023-04-06 17:30:12 +00:00
var dir = Directory.GetCurrentDirectory();
string? command;
while (true)
{
Console.WriteLine(@"cd - change directory
dir - show current directory contents
open - read data
mv - move data
rm - remove data
cp - copy data
info - get data info
");
Console.Write("{0}> ", dir);
command = Console.ReadLine();
switch (command)
{
case "cd":
2023-04-09 20:58:13 +00:00
dir = WorkWithFileSystem.Cd(UserInput.AskStringInput("Enter the directory name: "), dir);
2023-04-06 17:30:12 +00:00
break;
case "dir":
WorkWithFileSystem.GetDirectoryFiles(dir);
break;
case "open":
2023-04-09 20:58:13 +00:00
ReadFile.ReadByPath(UserInput.AskStringInput("Enter the file name: "));
2023-04-06 17:30:12 +00:00
break;
case "mv":
2023-04-09 20:58:13 +00:00
string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
string moveDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
2023-04-06 17:30:12 +00:00
2023-04-11 19:59:46 +00:00
try
{
moveDestination = Validation.Check(moveSource, moveDestination);
}
catch (PathConflictException)
{
Console.WriteLine("Conflict.");
break;
}
2023-04-06 17:30:12 +00:00
if (Directory.Exists(moveSource) || File.Exists(moveSource))
Directory.Move(moveSource, moveDestination);
else
Console.WriteLine("Not found.");
break;
case "rm":
2023-04-09 20:58:13 +00:00
string path = Path.Combine(dir, UserInput.AskStringInput("Enter the file or directory to delete: "));
2023-04-06 17:30:12 +00:00
if (Directory.Exists(path))
Directory.Delete(path, true);
else if (File.Exists(path))
File.Delete(path);
else
Console.WriteLine("Not found.");
break;
case "cp":
2023-04-09 20:58:13 +00:00
string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
string copyDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
2023-04-06 17:30:12 +00:00
2023-04-11 19:59:46 +00:00
try
{
copyDestination = Validation.Check(copySource, copyDestination);
}
catch (PathConflictException)
{
Console.WriteLine("Conflict.");
break;
}
2023-04-06 17:30:12 +00:00
if (Directory.Exists(copySource))
CopyDirectory.Copy(copySource, copyDestination, true);
else if (File.Exists(copySource))
File.Copy(copySource, copyDestination);
else
Console.WriteLine("Not found.");
break;
case "info":
2023-04-09 20:58:13 +00:00
FileInfoOperation.GetFileInfo(UserInput.AskStringInput("Enter the file name: "));
2023-04-06 17:30:12 +00:00
break;
default:
Console.WriteLine("Unknown command");
break;
}
}