Update Lesson 5 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-04-11 22:59:46 +03:00
parent 675692e965
commit 42aebfd7c7
GPG key ID: 1E95E66A9C9D6A36
3 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,19 @@
namespace Lesson5.Classes;
[Serializable]
public class PathConflictException : Exception
{
public PathConflictException ()
{
}
public PathConflictException (string message)
: base(message)
{
}
public PathConflictException (string message, Exception innerException)
: base(message, innerException)
{
}
}

View file

@ -0,0 +1,20 @@
namespace Lesson5.Classes;
using Lesson5.Interfaces;
public class Validation : IValidation
{
public static string Check(string source, string destination)
{
if (File.Exists(destination))
throw new PathConflictException();
if (Directory.Exists(destination))
destination = Path.Combine(destination, Path.GetFileName(source));
if (File.Exists(destination) || Directory.Exists(destination))
throw new PathConflictException();
return destination;
}
}

View file

@ -40,6 +40,16 @@ info - get data info
string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
string moveDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
try
{
moveDestination = Validation.Check(moveSource, moveDestination);
}
catch (PathConflictException)
{
Console.WriteLine("Conflict.");
break;
}
if (Directory.Exists(moveSource) || File.Exists(moveSource))
Directory.Move(moveSource, moveDestination);
else
@ -59,6 +69,16 @@ info - get data info
string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
string copyDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
try
{
copyDestination = Validation.Check(copySource, copyDestination);
}
catch (PathConflictException)
{
Console.WriteLine("Conflict.");
break;
}
if (Directory.Exists(copySource))
CopyDirectory.Copy(copySource, copyDestination, true);
else if (File.Exists(copySource))