diff --git a/Lesson_5/Task_1/Classes/PathConflictException.cs b/Lesson_5/Task_1/Classes/PathConflictException.cs new file mode 100644 index 0000000..82d2fcd --- /dev/null +++ b/Lesson_5/Task_1/Classes/PathConflictException.cs @@ -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) + { + } +} diff --git a/Lesson_5/Task_1/Classes/Validation.cs b/Lesson_5/Task_1/Classes/Validation.cs new file mode 100644 index 0000000..534592d --- /dev/null +++ b/Lesson_5/Task_1/Classes/Validation.cs @@ -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; + } +} diff --git a/Lesson_5/Task_1/Program.cs b/Lesson_5/Task_1/Program.cs index 69a4514..2f27dca 100644 --- a/Lesson_5/Task_1/Program.cs +++ b/Lesson_5/Task_1/Program.cs @@ -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))