Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to copy a folder and all subfolders and files using c#

Tags:

c#

I need to copy a Folder from one drive to a removable Hard disk. The Folder which needs to be copied will have many sub folders and files in it. The input will be Source Path and Target Path.

Like..

Source Path : "C:\SourceFolder"

Target Path : "E:\"

After copying is done, i shud be able to see the folder "SourceFolder" in my E: drive.

Thanks.

like image 807
Anuya Avatar asked Apr 30 '10 04:04

Anuya


People also ask

How do I copy a folder and subfolders?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

Which command can copy all files directories and subdirectories to another disk all at once?

Xcopy, stands for extended copy, is a command that can copy multiple files or entire directory trees from one location to another. As an advanced version of the copy command, it has additional switches to specify both the source and the destination in detail.

How do I copy all files and folders?

Open your folder, and select all the files ( Control + a or Command + a). Right-click and select Make a copy.


2 Answers

I think this is it.

public static void CopyFolder(DirectoryInfo source, DirectoryInfo target) {
    foreach (DirectoryInfo dir in source.GetDirectories())
        CopyFolder(dir, target.CreateSubdirectory(dir.Name));
    foreach (FileInfo file in source.GetFiles())
        file.CopyTo(Path.Combine(target.FullName, file.Name));
}
like image 141
Lukas Šalkauskas Avatar answered Sep 27 '22 20:09

Lukas Šalkauskas


Found this at Channel9. Haven't tried it myself.

public static class DirectoryInfoExtensions
{
    // Copies all files from one directory to another.
    public static void CopyTo(this DirectoryInfo source, 
            string destDirectory, bool recursive)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (destDirectory == null)
            throw new ArgumentNullException("destDirectory");
        // If the source doesn't exist, we have to throw an exception.
        if (!source.Exists)
            throw new DirectoryNotFoundException(
                    "Source directory not found: " + source.FullName);
        // Compile the target.
        DirectoryInfo target = new DirectoryInfo(destDirectory);
        // If the target doesn't exist, we create it.
        if (!target.Exists)
            target.Create();
        // Get all files and copy them over.
        foreach (FileInfo file in source.GetFiles())
        {
            file.CopyTo(Path.Combine(target.FullName, file.Name), true);
        }
        // Return if no recursive call is required.
        if (!recursive)
            return;
        // Do the same for all sub directories.
        foreach (DirectoryInfo directory in source.GetDirectories())
        {
            CopyTo(directory, 
                Path.Combine(target.FullName, directory.Name), recursive);
        }
    }
}

and the usage looks like this:

var source = new DirectoryInfo(@"C:\users\chris\desktop");
source.CopyTo(@"C:\users\chris\desktop_backup", true);
like image 31
egrunin Avatar answered Sep 27 '22 19:09

egrunin