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.



  


Leave a reply


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.

  • Arun says:
    Sep 30, 10

    Thanks a lot

  • b says:
    Oct 09, 10

    964899

  • Milo says:
    Oct 31, 10

    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

  • Vinod J says:
    Nov 12, 10

    Great !! it really helpful

  • Ignacio says:
    Dec 14, 10

    The preferred way of notifying errors in .Net is using exceptions not return values. Otherwise a handy function.

  • Ignacio says:
    Dec 14, 10

    The preferred way of notifying errors in .Net is using exceptions not return values. Otherwise a handy function.

  • thiru says:
    May 10, 11

    very useful.....

  • austine says:
    May 11, 11

    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

  • JJ says:
    May 14, 11

    um, please check your return value. if(ret == false) , code should return false immediately. otherwise the last ret = true will always be true.

  • Srikanth Kanchari says:
    Jan 12, 12

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

  • blackpearl says:
    Jan 25, 12

    Thank you so much. This post was very helpful.



Do you like this post?