diff --git a/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs b/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs new file mode 100644 index 0000000..0d0ba80 --- /dev/null +++ b/Lesson_5/Task_1/FileSystem/WorkWithFileSystem.cs @@ -0,0 +1,101 @@ +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/Program.cs b/Lesson_5/Task_1/Program.cs new file mode 100644 index 0000000..ad637ee --- /dev/null +++ b/Lesson_5/Task_1/Program.cs @@ -0,0 +1,78 @@ +/* +* Lesson 5 Task 1: a file manager +* Author: Stanislav Mykhailenko, parts of code used from DanyilMykytenko's repository +* License: Unlicense +*/ + + +using Lesson5_WorkWithFiles.FileSystem; +using Lesson5_WorkWithFiles.WorkWithFiles; +using Lesson5_WorkWithFiles.CopyDirectory; + +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": + dir = WorkWithFileSystem.Cd(WorkWithFileSystem.AskStringInput("Enter the directory name: "), dir); + break; + case "dir": + WorkWithFileSystem.GetDirectoryFiles(dir); + break; + case "open": + ReadWrite.ReadByPath(WorkWithFileSystem.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: ")); + + if (Directory.Exists(moveSource) || File.Exists(moveSource)) + Directory.Move(moveSource, moveDestination); + else + Console.WriteLine("Not found."); + break; + case "rm": + string path = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the file or directory to delete: ")); + + if (Directory.Exists(path)) + Directory.Delete(path, true); + else if (File.Exists(path)) + File.Delete(path); + else + 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: ")); + + 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": + FileInfoOperation.GetFileInfo(WorkWithFileSystem.AskStringInput("Enter the file name: ")); + break; + default: + Console.WriteLine("Unknown command"); + break; + } +} diff --git a/Lesson_5/Task_1/Task_1.csproj b/Lesson_5/Task_1/Task_1.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/Lesson_5/Task_1/Task_1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs b/Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs new file mode 100644 index 0000000..4ecdbe7 --- /dev/null +++ b/Lesson_5/Task_1/WorkWithFiles/CopyDirectory.cs @@ -0,0 +1,37 @@ +namespace Lesson5_WorkWithFiles.CopyDirectory; + +public class CopyDirectory +{ + public static void Copy(string sourceDir, string destinationDir, bool recursive) + { + // Get information about the source directory + var dir = new DirectoryInfo(sourceDir); + + // Check if the source directory exists + if (!dir.Exists) + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); + + // Cache directories before we start copying + DirectoryInfo[] dirs = dir.GetDirectories(); + + // Create the destination directory + Directory.CreateDirectory(destinationDir); + + // Get the files in the source directory and copy to the destination directory + foreach (FileInfo file in dir.GetFiles()) + { + string targetFilePath = Path.Combine(destinationDir, file.Name); + file.CopyTo(targetFilePath); + } + + // If recursive and copying subdirectories, recursively call this method + if (recursive) + { + foreach (DirectoryInfo subDir in dirs) + { + string newDestinationDir = Path.Combine(destinationDir, subDir.Name); + Copy(subDir.FullName, newDestinationDir, true); + } + } + } +} diff --git a/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs b/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs new file mode 100644 index 0000000..f180adc --- /dev/null +++ b/Lesson_5/Task_1/WorkWithFiles/FileInfoOperation.cs @@ -0,0 +1,31 @@ +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/ReadWrite.cs b/Lesson_5/Task_1/WorkWithFiles/ReadWrite.cs new file mode 100644 index 0000000..257d0a4 --- /dev/null +++ b/Lesson_5/Task_1/WorkWithFiles/ReadWrite.cs @@ -0,0 +1,19 @@ +namespace Lesson5_WorkWithFiles.WorkWithFiles; + +public static class ReadWrite +{ + public static void ReadByPath(string path) + { + var text = File.ReadLines(path); + + foreach (var str in text) + { + Console.WriteLine(str); + } + } + + public static void WriteToFile(string text, string path) + { + File.WriteAllText(text, path); + } +} diff --git a/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs b/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs new file mode 100644 index 0000000..fcefe30 --- /dev/null +++ b/Lesson_5/Task_1/WorkWithFiles/StreamUsing.cs @@ -0,0 +1,30 @@ +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"); + } +} diff --git a/README.md b/README.md index 34d24f7..8d2c2eb 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,7 @@ Checked means the task is Done. - [ ] Task 1 - [ ] Task 2 + +### Lesson 5 + +- [ ] Task 1