Advertisement
kyrathasoft

copy files from one directory to another

May 21st, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1.     public static bool CopyTo(string targetDirectory, string destinationDirectory)
  2.     {
  3.         bool blnSuccess = false;
  4.        
  5.         try{
  6.             if(Directory.Exists(targetDirectory)){
  7.                 foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*",
  8.                     SearchOption.AllDirectories)){
  9.                     Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory)); 
  10.                 }
  11.                 //Copy all the files & Replaces any files with the same name
  12.                 foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*",
  13.                     SearchOption.AllDirectories)){
  14.                     File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true);
  15.                     blnSuccess = true;
  16.                 }                      
  17.             }              
  18.         }catch(Exception exCopyTo){
  19.             Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message);
  20.         }
  21.        
  22.         return blnSuccess;
  23.        
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement