Copying Folder And Files


This code help you to copy all folders, sub folders and files from one location to other.

This code help you to copy all folders, sub folders and files from one location to other.


private static Boolean CopyNewCacheFolder(string SourcePath, string DestinationPath, bool overwriteexisting)
{
Boolean ret = false;
try
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";


if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);

foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (CopyNewCacheFolder(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
ret = false;
}
}
ret = true;
}
catch (Exception ex)
{
ret = false;
}
return ret;
}


Comments

Author: Umesh Bhosale01 Apr 2014 Member Level: Silver   Points : 0

Hello

You can also Provide Filter For Directory.GetFiles("Path","Filer");

e.g:
Directory.GetFiles("D:\TestFiles","*.xml");
it will always give XML file form same folder.

Thanks
Umesh



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: