Update Lesson 5 Task 1
This commit is contained in:
parent
bbce5fb19c
commit
fd95e46b80
14 changed files with 125 additions and 183 deletions
|
@ -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)
|
public static void Copy(string sourceDir, string destinationDir, bool recursive)
|
||||||
{
|
{
|
16
Lesson_5/Task_1/Classes/FileInfoOperation.cs
Normal file
16
Lesson_5/Task_1/Classes/FileInfoOperation.cs
Normal file
|
@ -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}");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
public static void ReadByPath(string path)
|
||||||
{
|
{
|
||||||
|
@ -11,9 +13,4 @@ public static class ReadWrite
|
||||||
Console.WriteLine(str);
|
Console.WriteLine(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteToFile(string text, string path)
|
|
||||||
{
|
|
||||||
File.WriteAllText(text, path);
|
|
||||||
}
|
|
||||||
}
|
}
|
17
Lesson_5/Task_1/Classes/UserInput.cs
Normal file
17
Lesson_5/Task_1/Classes/UserInput.cs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
Lesson_5/Task_1/Classes/WorkWithFileSystem.cs
Normal file
43
Lesson_5/Task_1/Classes/WorkWithFileSystem.cs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string>();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
6
Lesson_5/Task_1/Interfaces/ICopyDirectory.cs
Normal file
6
Lesson_5/Task_1/Interfaces/ICopyDirectory.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Lesson5.Interfaces;
|
||||||
|
|
||||||
|
public interface ICopyDirectory
|
||||||
|
{
|
||||||
|
public static abstract void Copy(string sourceDir, string destinationDir, bool recursive);
|
||||||
|
}
|
6
Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs
Normal file
6
Lesson_5/Task_1/Interfaces/IFileInfoOperation.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Lesson5.Interfaces;
|
||||||
|
|
||||||
|
public interface IFileInfoOperation
|
||||||
|
{
|
||||||
|
public static abstract void GetFileInfo(string path);
|
||||||
|
}
|
6
Lesson_5/Task_1/Interfaces/IReadFile.cs
Normal file
6
Lesson_5/Task_1/Interfaces/IReadFile.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Lesson5.Interfaces;
|
||||||
|
|
||||||
|
public interface IReadFile
|
||||||
|
{
|
||||||
|
public static abstract void ReadByPath(string path);
|
||||||
|
}
|
6
Lesson_5/Task_1/Interfaces/IUserInput.cs
Normal file
6
Lesson_5/Task_1/Interfaces/IUserInput.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Lesson5.Interfaces;
|
||||||
|
|
||||||
|
public interface IUserInput
|
||||||
|
{
|
||||||
|
public static abstract string AskStringInput(string prompt);
|
||||||
|
}
|
7
Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs
Normal file
7
Lesson_5/Task_1/Interfaces/IWorkWithFileSystem.cs
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -4,10 +4,8 @@
|
||||||
* License: Unlicense
|
* License: Unlicense
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Lesson5.Classes;
|
||||||
using Lesson5_WorkWithFiles.FileSystem;
|
using Lesson5.Interfaces;
|
||||||
using Lesson5_WorkWithFiles.WorkWithFiles;
|
|
||||||
using Lesson5_WorkWithFiles.CopyDirectory;
|
|
||||||
|
|
||||||
var dir = Directory.GetCurrentDirectory();
|
var dir = Directory.GetCurrentDirectory();
|
||||||
string? command;
|
string? command;
|
||||||
|
@ -30,17 +28,17 @@ info - get data info
|
||||||
switch (command)
|
switch (command)
|
||||||
{
|
{
|
||||||
case "cd":
|
case "cd":
|
||||||
dir = WorkWithFileSystem.Cd(WorkWithFileSystem.AskStringInput("Enter the directory name: "), dir);
|
dir = WorkWithFileSystem.Cd(UserInput.AskStringInput("Enter the directory name: "), dir);
|
||||||
break;
|
break;
|
||||||
case "dir":
|
case "dir":
|
||||||
WorkWithFileSystem.GetDirectoryFiles(dir);
|
WorkWithFileSystem.GetDirectoryFiles(dir);
|
||||||
break;
|
break;
|
||||||
case "open":
|
case "open":
|
||||||
ReadWrite.ReadByPath(WorkWithFileSystem.AskStringInput("Enter the file name: "));
|
ReadFile.ReadByPath(UserInput.AskStringInput("Enter the file name: "));
|
||||||
break;
|
break;
|
||||||
case "mv":
|
case "mv":
|
||||||
string moveSource = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the source: "));
|
string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
||||||
string moveDestination = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the destination: "));
|
string moveDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
|
||||||
|
|
||||||
if (Directory.Exists(moveSource) || File.Exists(moveSource))
|
if (Directory.Exists(moveSource) || File.Exists(moveSource))
|
||||||
Directory.Move(moveSource, moveDestination);
|
Directory.Move(moveSource, moveDestination);
|
||||||
|
@ -48,7 +46,7 @@ info - get data info
|
||||||
Console.WriteLine("Not found.");
|
Console.WriteLine("Not found.");
|
||||||
break;
|
break;
|
||||||
case "rm":
|
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))
|
if (Directory.Exists(path))
|
||||||
Directory.Delete(path, true);
|
Directory.Delete(path, true);
|
||||||
|
@ -58,8 +56,8 @@ info - get data info
|
||||||
Console.WriteLine("Not found.");
|
Console.WriteLine("Not found.");
|
||||||
break;
|
break;
|
||||||
case "cp":
|
case "cp":
|
||||||
string copySource = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the source: "));
|
string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
||||||
string copyDestination = Path.Combine(dir, WorkWithFileSystem.AskStringInput("Enter the destination: "));
|
string copyDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
|
||||||
|
|
||||||
if (Directory.Exists(copySource))
|
if (Directory.Exists(copySource))
|
||||||
CopyDirectory.Copy(copySource, copyDestination, true);
|
CopyDirectory.Copy(copySource, copyDestination, true);
|
||||||
|
@ -69,7 +67,7 @@ info - get data info
|
||||||
Console.WriteLine("Not found.");
|
Console.WriteLine("Not found.");
|
||||||
break;
|
break;
|
||||||
case "info":
|
case "info":
|
||||||
FileInfoOperation.GetFileInfo(WorkWithFileSystem.AskStringInput("Enter the file name: "));
|
FileInfoOperation.GetFileInfo(UserInput.AskStringInput("Enter the file name: "));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Console.WriteLine("Unknown command");
|
Console.WriteLine("Unknown command");
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue