Copy Directory from one location to other Recursively

Posted by Lakhangarg under C# category on | Views : 2444
In this Method we need to pass the Path of Source Directory and Destination directorty.
and this function will copy all the directories and file in that directory to a new location.
public void copyDirectory(string Src, string Dst)
String[] Files;
if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files = Directory.GetFileSystemEntries(Src);
foreach (string Element in Files)
// Sub directories
if (Directory.Exists(Element))
copyDirectory(Element, Dst + Path.GetFileName(Element));
// Files in directory
File.Copy(Element, Dst + Path.GetFileName(Element), true);

Thanks & Regards
Lakhan Pal Garg

Comments or Responses

Login to post response