The below "CopyDirectory" function in C# can be used to copy folders/directory from source path to a destination path.
The below function would copy all sub-folders from source folders to destination.
The Destination folder would be created if it does not exists.
"overwriteexisting" boolean parameter can used to set overwrite option while copying.
First Directory.GetFiles method in System.IO namespace is used to get list of all files from source folder.
FileInfo class CopyTo method in System.IO namespace is used to copy each file source folder to destination.
Next Sub-folders is copied to destination location by calling CopyDirectory recursively.
The "CopyDirectory" would return bool value whether copying files/folders is successful or not.
Namespace used:System.IO
private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
{
bool 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 (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
ret = false;
}
}
ret = true;
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
Using this function
bool copy = CopyDirectory("c:\\temp\\index\\", "c:\\temp\\newindex\\", true);
This would copy all directories and sub-directories from "c:\\temp\\index\\" to "c:\\temp\\newindex\\" with overwrite option set.
Thanks alot for posting this function!!!
I'll second that! This was extremely useful for me.
or, an easier way of doing the same, non recursive private static void CopyDirectory(string trgDir, string srcDir) { string[] allFiles = Directory.GetFiles(srcDir, "*.*", SearchOption.AllDirectories); foreach (string srcFile in allFiles) { string targetFile = trgDir + srcFile.Substring(srcDir.Length); string targetDir = Path.GetDirectoryName(targetFile); if (Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } File.Copy(srcFile, targetFile); } }
Thanks a lot for the code! It's very useful for me.. @ka2: your code seems much simpler, but requires more execution time.
with VB6/vbscript it was copy "c:\*.*" Ever wonder if things have become less productive?
Thanx It's very helfull function.
Thanks a lot
964899
Damn, there are some ugly examples.. Try this elegant little number. http://01792.org/blog/post/2010/10/06/Recursive-file-and-folder-copy.aspx
Great !! it really helpful
The preferred way of notifying errors in .Net is using exceptions not return values. Otherwise a handy function.
The preferred way of notifying errors in .Net is using exceptions not return values. Otherwise a handy function.
very useful.....
Hi just wanted to ask on how to copy file from local c drive to ftp server I tried this sample but not working with . Please help. Austine
um, please check your return value. if(ret == false) , code should return false immediately. otherwise the last ret = true will always be true.
private bool XCopy(String source,String destination) { DirectoryInfo sourcedir = new DirectoryInfo(source); DirectoryInfo destinationdir = new DirectoryInfo(destination); if (!Directory.Exists(destinationdir.FullName)) { Directory.CreateDirectory(destinationdir.FullName); } foreach (FileInfo fs in sourcedir.GetFiles()) { String dest = destinationdir.FullName + "\\" + fs.Name; File.Copy(fs.FullName, dest, true); } foreach (DirectoryInfo ds in sourcedir.GetDirectories()) { String newdir = destinationdir.FullName + "\\" +ds.Name; XCopy(ds.FullName, newdir); } return true; }
Thank you so much. This post was very helpful.