Add Lesson 5 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-04-06 20:30:12 +03:00
parent db251c4838
commit 9ad0e7cbb0
GPG key ID: 1E95E66A9C9D6A36
8 changed files with 310 additions and 0 deletions

View file

@ -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<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;
}
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -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);
}
}
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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");
}
}

View file

@ -29,3 +29,7 @@ Checked means the task is Done.
- [ ] Task 1 - [ ] Task 1
- [ ] Task 2 - [ ] Task 2
### Lesson 5
- [ ] Task 1