From fd95e46b800a07d06a3ca0954653c49a5df0c2e7 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sun, 9 Apr 2023 23:58:13 +0300 Subject: [PATCH] Update Lesson 5 Task 1 --- .../CopyDirectory.cs | 6 +- Lesson_5/Task_1/Classes/FileInfoOperation.cs | 16 +++ .../ReadWrite.cs => Classes/ReadFile.cs} | 11 +- Lesson_5/Task_1/Classes/UserInput.cs | 17 +++ Lesson_5/Task_1/Classes/WorkWithFileSystem.cs | 43 ++++++++ .../Task_1/FileSystem/WorkWithFileSystem.cs | 101 ------------------ Lesson_5/Task_1/Interfaces/ICopyDirectory.cs | 6 ++ .../Task_1/Interfaces/IFileInfoOperation.cs | 6 ++ Lesson_5/Task_1/Interfaces/IReadFile.cs | 6 ++ Lesson_5/Task_1/Interfaces/IUserInput.cs | 6 ++ .../Task_1/Interfaces/IWorkWithFileSystem.cs | 7 ++ Lesson_5/Task_1/Program.cs | 22 ++-- .../Task_1/WorkWithFiles/FileInfoOperation.cs | 31 ------ Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs | 30 ------ 14 files changed, 125 insertions(+), 183 deletions(-) rename Lesson_5/Task_1/{WorkWithFiles => Classes}/CopyDirectory.cs (92%) create mode 100644 Lesson_5/Task_1/Classes/FileInfoOperation.cs rename Lesson_5/Task_1/{WorkWithFiles/ReadWrite.cs => Classes/ReadFile.cs} (51%) create mode 100644 Lesson_5/Task_1/Classes/UserInput.cs create mode 100644 Lesson_5/Task_1/Classes/WorkWithFileSystem.cs delete mode 100644 Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs create mode 100644 Lesson_5/Task_1/Interfaces/ICopyDirectory.cs create mode 100644 Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs create mode 100644 Lesson_5/Task_1/Interfaces/IReadFile.cs create mode 100644 Lesson_5/Task_1/Interfaces/IUserInput.cs create mode 100644 Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs delete mode 100644 Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs delete mode 100644 Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs diff --git a/Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs b/Lesson_5/Task_1/Classes/CopyDirectory.cs similarity index 92% rename from Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs rename to Lesson_5/Task_1/Classes/CopyDirectory.cs index 4ecdbe7..2d4a63b 100644 --- a/Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs +++ b/Lesson_5/Task_1/Classes/CopyDirectory.cs @@ -1,6 +1,8 @@ -namespace Lesson5_WorkWithFiles.CopyDirectory; +namespace Lesson5.Classes; -public class CopyDirectory +using Lesson5.Interfaces; + +public class CopyDirectory : ICopyDirectory { public static void Copy(string sourceDir, string destinationDir, bool recursive) { diff --git a/Lesson_5/Task_1/Classes/FileInfoOperation.cs b/Lesson_5/Task_1/Classes/FileInfoOperation.cs new file mode 100644 index 0000000..152adf6 --- /dev/null +++ b/Lesson_5/Task_1/Classes/FileInfoOperation.cs @@ -0,0 +1,16 @@ +namespace Lesson5.Classes; + +using Lesson5.Interfaces; + +public class FileInfoOperation : IFileInfoOperation +{ + + public static void GetFileInfo(string path) + { + var info = new FileInfo(path); + + Console.WriteLine($"Name: {info.Name}"); + Console.WriteLine($"Creation time: {info.CreationTime}"); + Console.WriteLine($"Length: {info.Length}"); + } +} diff --git a/Lesson_5/Task_1/WorkWithFiles/ReadWrite.cs b/Lesson_5/Task_1/Classes/ReadFile.cs similarity index 51% rename from Lesson_5/Task_1/WorkWithFiles/ReadWrite.cs rename to Lesson_5/Task_1/Classes/ReadFile.cs index 257d0a4..cc606d1 100644 --- a/Lesson_5/Task_1/WorkWithFiles/ReadWrite.cs +++ b/Lesson_5/Task_1/Classes/ReadFile.cs @@ -1,6 +1,8 @@ -namespace Lesson5_WorkWithFiles.WorkWithFiles; +namespace Lesson5.Classes; -public static class ReadWrite +using Lesson5.Interfaces; + +public class ReadFile : IReadFile { public static void ReadByPath(string path) { @@ -11,9 +13,4 @@ public static class ReadWrite Console.WriteLine(str); } } - - public static void WriteToFile(string text, string path) - { - File.WriteAllText(text, path); - } } diff --git a/Lesson_5/Task_1/Classes/UserInput.cs b/Lesson_5/Task_1/Classes/UserInput.cs new file mode 100644 index 0000000..30aa9a0 --- /dev/null +++ b/Lesson_5/Task_1/Classes/UserInput.cs @@ -0,0 +1,17 @@ +namespace Lesson5.Classes; + +using Lesson5.Interfaces; + +public class UserInput : IUserInput +{ + public static string AskStringInput(string prompt) + { + while (true) + { + Console.Write(prompt); + string? input = Console.ReadLine(); + if (!string.IsNullOrEmpty(input)) + return input; + } + } +} diff --git a/Lesson_5/Task_1/Classes/WorkWithFileSystem.cs b/Lesson_5/Task_1/Classes/WorkWithFileSystem.cs new file mode 100644 index 0000000..5ec8aad --- /dev/null +++ b/Lesson_5/Task_1/Classes/WorkWithFileSystem.cs @@ -0,0 +1,43 @@ +namespace Lesson5.Classes; + +using Lesson5.Interfaces; + +public class WorkWithFileSystem : IWorkWithFileSystem +{ + public static void GetDirectoryFiles(string path) + { + var current = new DirectoryInfo(path); + var dirs = Directory.GetDirectories(current.FullName); + var files = Directory.GetFiles(current.FullName); + + foreach (var dir in dirs) + { + Console.WriteLine(dir.Split('\\').Last() + "/"); + } + foreach (var file in files) + { + Console.WriteLine(file.Split('\\').Last()); + } + } + + public static string Cd(string name, string current) + { + if (name == "..") + { + var info = new DirectoryInfo(current); + + var parent = info.Parent; + + return parent is not null ? parent.FullName : current; + } + + var dir = Path.Combine(current, name); + + if (Directory.Exists(dir)) + { + return dir; + } + + return current; + } +} diff --git a/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs b/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs deleted file mode 100644 index 0d0ba80..0000000 --- a/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs +++ /dev/null @@ -1,101 +0,0 @@ -namespace Lesson5_WorkWithFiles.FileSystem; - -public static class WorkWithFileSystem -{ - public static void GetDrives() - { - var drives = DriveInfo.GetDrives(); - - foreach (var drive in drives) - { - Console.WriteLine($"Name: {drive.Name}"); - Console.WriteLine($"Size: {drive.TotalSize * Math.Pow(10, -9) }"); - Console.WriteLine($"Free: {drive.AvailableFreeSpace * Math.Pow(10, -9) }"); - - Console.WriteLine("-------------------"); - } - } - - public static void GetCurrentDirectoryFiles() - { - var dir = Directory.GetCurrentDirectory(); - var files = new List(); - files.AddRange(Directory.GetDirectories(dir)); - files.AddRange(Directory.GetFiles(dir)); - - foreach (var file in files) - { - Console.WriteLine(file.Split('\\').Last()); - } - } - - // cd variant with SetCurrentDirectoryMethod - public static void SetCurrentDirectory(string name, string current) - { - if (name == "..") - { - var info = new DirectoryInfo(current); - - var parent = info.Parent; - - if(parent is null) return; - - Directory.SetCurrentDirectory(parent.FullName); - } - - var dir = Path.Combine(current, name); - - if (Directory.Exists(dir)) - { - Directory.SetCurrentDirectory(dir); - } - } - - public static void GetDirectoryFiles(string path) - { - var current = new DirectoryInfo(path); - var dirs = Directory.GetDirectories(current.FullName); - var files = Directory.GetFiles(current.FullName); - - foreach (var dir in dirs) - { - Console.WriteLine(dir.Split('\\').Last() + "/"); - } - foreach (var file in files) - { - Console.WriteLine(file.Split('\\').Last()); - } - } - - public static string Cd(string name, string current) - { - if (name == "..") - { - var info = new DirectoryInfo(current); - - var parent = info.Parent; - - return parent is not null ? parent.FullName : current; - } - - var dir = Path.Combine(current, name); - - if (Directory.Exists(dir)) - { - return dir; - } - - return current; - } - - public static string AskStringInput(string prompt) - { - while (true) - { - Console.Write(prompt); - string? input = Console.ReadLine(); - if (!string.IsNullOrEmpty(input)) - return input; - } - } -} diff --git a/Lesson_5/Task_1/Interfaces/ICopyDirectory.cs b/Lesson_5/Task_1/Interfaces/ICopyDirectory.cs new file mode 100644 index 0000000..81d7c3b --- /dev/null +++ b/Lesson_5/Task_1/Interfaces/ICopyDirectory.cs @@ -0,0 +1,6 @@ +namespace Lesson5.Interfaces; + +public interface ICopyDirectory +{ + public static abstract void Copy(string sourceDir, string destinationDir, bool recursive); +} diff --git a/Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs b/Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs new file mode 100644 index 0000000..cf799e8 --- /dev/null +++ b/Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs @@ -0,0 +1,6 @@ +namespace Lesson5.Interfaces; + +public interface IFileInfoOperation +{ + public static abstract void GetFileInfo(string path); +} diff --git a/Lesson_5/Task_1/Interfaces/IReadFile.cs b/Lesson_5/Task_1/Interfaces/IReadFile.cs new file mode 100644 index 0000000..fdd429f --- /dev/null +++ b/Lesson_5/Task_1/Interfaces/IReadFile.cs @@ -0,0 +1,6 @@ +namespace Lesson5.Interfaces; + +public interface IReadFile +{ + public static abstract void ReadByPath(string path); +} diff --git a/Lesson_5/Task_1/Interfaces/IUserInput.cs b/Lesson_5/Task_1/Interfaces/IUserInput.cs new file mode 100644 index 0000000..10e579a --- /dev/null +++ b/Lesson_5/Task_1/Interfaces/IUserInput.cs @@ -0,0 +1,6 @@ +namespace Lesson5.Interfaces; + +public interface IUserInput +{ + public static abstract string AskStringInput(string prompt); +} diff --git a/Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs b/Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs new file mode 100644 index 0000000..c09cbaa --- /dev/null +++ b/Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs @@ -0,0 +1,7 @@ +namespace Lesson5.Interfaces; + +public interface IWorkWithFileSystem +{ + public static abstract void GetDirectoryFiles(string path); + public static abstract string Cd(string name, string current); +} diff --git a/Lesson_5/Task_1/Program.cs b/Lesson_5/Task_1/Program.cs index ad637ee..69a4514 100644 --- a/Lesson_5/Task_1/Program.cs +++ b/Lesson_5/Task_1/Program.cs @@ -4,10 +4,8 @@ * License: Unlicense */ - -using Lesson5_WorkWithFiles.FileSystem; -using Lesson5_WorkWithFiles.WorkWithFiles; -using Lesson5_WorkWithFiles.CopyDirectory; +using Lesson5.Classes; +using Lesson5.Interfaces; var dir = Directory.GetCurrentDirectory(); string? command; @@ -30,17 +28,17 @@ info - get data info switch (command) { case "cd": - dir = WorkWithFileSystem.Cd(WorkWithFileSystem.AskStringInput("Enter the directory name: "), dir); + dir = WorkWithFileSystem.Cd(UserInput.AskStringInput("Enter the directory name: "), dir); break; case "dir": WorkWithFileSystem.GetDirectoryFiles(dir); break; case "open": - ReadWrite.ReadByPath(WorkWithFileSystem.AskStringInput("Enter the file name: ")); + ReadFile.ReadByPath(UserInput.AskStringInput("Enter the file name: ")); break; case "mv": - string moveSource = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the source: ")); - string moveDestination = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the destination: ")); + string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: ")); + string moveDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: ")); if (Directory.Exists(moveSource) || File.Exists(moveSource)) Directory.Move(moveSource, moveDestination); @@ -48,7 +46,7 @@ info - get data info Console.WriteLine("Not found."); break; case "rm": - string path = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the file or directory to delete: ")); + string path = Path.Combine(dir, UserInput.AskStringInput("Enter the file or directory to delete: ")); if (Directory.Exists(path)) Directory.Delete(path, true); @@ -58,8 +56,8 @@ info - get data info Console.WriteLine("Not found."); break; case "cp": - string copySource = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the source: ")); - string copyDestination = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the destination: ")); + string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: ")); + string copyDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: ")); if (Directory.Exists(copySource)) CopyDirectory.Copy(copySource, copyDestination, true); @@ -69,7 +67,7 @@ info - get data info Console.WriteLine("Not found."); break; case "info": - FileInfoOperation.GetFileInfo(WorkWithFileSystem.AskStringInput("Enter the file name: ")); + FileInfoOperation.GetFileInfo(UserInput.AskStringInput("Enter the file name: ")); break; default: Console.WriteLine("Unknown command"); diff --git a/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs b/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs deleted file mode 100644 index f180adc..0000000 --- a/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace Lesson5_WorkWithFiles.WorkWithFiles; - -public static class FileInfoOperation -{ - public static string CreateFile(string dir, string name) - { - var path = Path.Combine(dir, name); - - var file = new FileInfo(path); - if (!file.Exists) file.Create(); - - return file.FullName; - } - - public static void GetFileInfo(string path) - { - var info = new FileInfo(path); - - Console.WriteLine($"Name: {info.Name}"); - Console.WriteLine($"Creation time: {info.CreationTime}"); - Console.WriteLine($"Length: {info.Length}"); - } - - public static void MoveFile(string filePath, string newPath) - { - var fileInfo = new FileInfo(filePath); - newPath = Path.Combine(newPath, fileInfo.Name); - - fileInfo.MoveTo(newPath); - } -} diff --git a/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs b/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs deleted file mode 100644 index fcefe30..0000000 --- a/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Text; - -namespace Lesson5_WorkWithFiles.WorkWithFiles; - -public static class StreamUsing -{ - public static void ReadFromFile(string path) - { - using (FileStream fstream = File.OpenRead(path)) - { - var bytes = new byte[fstream.Length]; - - fstream.Read(bytes, 0, bytes.Length); - - string result = Encoding.Default.GetString(bytes); - Console.WriteLine(result); - } - } - - public static void WriteToFile(string path, string text) - { - // you can write using in one line - - using FileStream fstream = new FileStream(path, FileMode.OpenOrCreate); - var bytes = Encoding.Default.GetBytes(text); - - fstream.Write(bytes, 0, bytes.Length); - Console.WriteLine("Written"); - } -}