andrew4582

CopyDirectory

Feb 25th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1.         private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting = true)
  2.         {
  3.             bool ret = false;
  4.             try
  5.             {
  6.                 SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
  7.                 DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
  8.  
  9.                 if (Directory.Exists(SourcePath))
  10.                 {
  11.                     if (Directory.Exists(DestinationPath) == false)
  12.                         Directory.CreateDirectory(DestinationPath);
  13.  
  14.                     foreach (string fls in Directory.GetFiles(SourcePath))
  15.                     {
  16.                         FileInfo flinfo = new FileInfo(fls);
  17.                         flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
  18.                     }
  19.                     foreach (string drs in Directory.GetDirectories(SourcePath))
  20.                     {
  21.                         DirectoryInfo drinfo = new DirectoryInfo(drs);
  22.                         if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
  23.                             ret = false;
  24.                     }
  25.                 }
  26.                 ret = true;
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 ret = false;
  31.             }
  32.             return ret;
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment