Alex Rivera | Logout

Copy Folders in C# using System.IO

Asked 2009-03-24T12:47:28.990
18

I need to Copy folder C:\FromFolder to C:\ToFolder

Below is code that will CUT my FromFolder and then will create my ToFolder. So my FromFolder will be gone and all the items will be in the newly created folder called ToFolder

System.IO.Directory.Move(@"C:\FromFolder ", @"C:\ToFolder");

But i just want to Copy the files in FromFolder to ToFolder. For some reason there is no System.IO.Directory.Copy???

How this is done using a batch file - Very easy

xcopy C:\FromFolder C:\ToFolder

Regards Etienne

Edit
Report

4 Answers

5

Copying directories (correctly) is actually a rather complex task especially if you take into account advanced filesystem techniques like junctions and hard links. Your best bet is to use an API that supports it. If you aren't afraid of a little P/Invoke, SHFileOperation in shell32 is your best bet. Another alternative would be to use the Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory method in the Microsoft.VisualBasic assembly (even if you aren't using VB).

answered 2009-03-24T14:17:16.700
2

You'll need to create a new directory from scratch then loop through all the files in the source directory and copy them over.

string[] files = Directory.GetFiles(GlobalVariables.mstrReadsWellinPath);
foreach(string s in files)
{
        fileName=Path.GetFileName(s);
        destFile = Path.Combine(DestinationPath, fileName);
        File.Copy(s, destFile);
}

I leave creating the destination directory to you :-)

answered 2009-03-24T12:53:12.037
1

This article provides an alogirthm to copy recursively some folder and all its content

From the article :

Sadly there is no built-in function in System.IO that will copy a folder and its contents. Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed. For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.

Good luck!

answered 2009-03-24T12:54:19.973
0

My version of DirectoryInfo.CopyTo using extension.

public static class DirectoryInfoEx {
    public static void CopyTo(this DirectoryInfo source, DirectoryInfo target) {
        if (source.FullName.ToLower() == target.FullName.ToLower())
            return;

        if (!target.Exists)
            target.Create();

        foreach (FileInfo f in source.GetFiles()) {
            FileInfo newFile = new FileInfo(Path.Combine(target.FullName, f.Name));
            f.CopyTo(newFile.FullName, true);
        }

        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
            DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
            diSourceSubDir.CopyTo(nextTargetSubDir);
        }
    }
}

And use like that...

DirectoryInfo d = new DirectoryInfo("C:\Docs");
d.CopyTo(new DirectoryInfo("C:\New"));
answered 2013-05-09T21:12:31.030

Your Answer