Custom Search
Logiclabz
  • Home
  • C#
  • Copy Directory in .Net C# including sub-folders

Copy Directory in .Net C# including sub-folders

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.


Comments

  • Tmann says:
    May 05, 09

    Thanks alot for posting this function!!!

  • James says:
    Jun 08, 09

    I'll second that! This was extremely useful for me.

  • ka2 says:
    Dec 17, 09

    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); } }

  • Faris says:
    Jan 20, 10

    Thanks a lot for the code! It's very useful for me.. @ka2: your code seems much simpler, but requires more execution time.

  • bort says:
    Mar 12, 10

    with VB6/vbscript it was copy "c:\*.*" Ever wonder if things have become less productive?

  • Sandeep K Midgule says:
    May 04, 10

    Thanx It's very helfull function.


Leave a reply


Do you like this post?