Update Lesson 5 Task 1
This commit is contained in:
parent
675692e965
commit
42aebfd7c7
3 changed files with 59 additions and 0 deletions
19
Lesson_5/Task_1/Classes/PathConflictException.cs
Normal file
19
Lesson_5/Task_1/Classes/PathConflictException.cs
Normal 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
20
Lesson_5/Task_1/Classes/Validation.cs
Normal file
20
Lesson_5/Task_1/Classes/Validation.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,6 +40,16 @@ info - get data info
|
||||||
string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
string moveSource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
||||||
string moveDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
|
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))
|
if (Directory.Exists(moveSource) || File.Exists(moveSource))
|
||||||
Directory.Move(moveSource, moveDestination);
|
Directory.Move(moveSource, moveDestination);
|
||||||
else
|
else
|
||||||
|
@ -59,6 +69,16 @@ info - get data info
|
||||||
string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
string copySource = Path.Combine(dir, UserInput.AskStringInput("Enter the source: "));
|
||||||
string copyDestination = Path.Combine(dir, UserInput.AskStringInput("Enter the destination: "));
|
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))
|
if (Directory.Exists(copySource))
|
||||||
CopyDirectory.Copy(copySource, copyDestination, true);
|
CopyDirectory.Copy(copySource, copyDestination, true);
|
||||||
else if (File.Exists(copySource))
|
else if (File.Exists(copySource))
|
||||||
|
|
Reference in a new issue