Click here to Skip to main content
15,306,686 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I am having a directory multiple inner directories and files.
I need to copy the directory having all the files to another location using C#.

How to do this
Updated 3-May-19 9:38am

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search gave over 3 million results: Google "directory copy c#"[^]
The top result was MSDN with a full example:[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
Mitchell J. 3-Apr-14 7:57am
Try like this sample code;

using System;
using System.IO;

class DirectoryCopyExample
    static void Main()
        // Copy from the current directory, include subdirectories.
        DirectoryCopy(".", @".\temp", true);

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        if (!dir.Exists)
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);

        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
            foreach (DirectoryInfo subdir in dirs)
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
Member 14898909 25-Jul-20 16:22pm
thnx man your code really helped me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900