---
title: "How do you copy all files from one directory to another, preserving the structure?"  
description: "How do you copy all files from one directory to another, preserving the structure?"  
author: "ICSM Computer"  
published: 2025-05-12  
updated: 2025-05-28  
canonical: https://www.mindstick.com/forum/161603/how-do-you-copy-all-files-from-one-directory-to-another-preserving-the-structure  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you copy all files from one directory to another, preserving the structure?

How do you [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) all [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) from one [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) to another, preserving the [structure](https://www.mindstick.com/articles/23258/choose-your-business-structure-wisely)?

## Replies

### Reply by Utpal Vishwas

To copy all files from one directory to another while **preserving the directory structure**, you can use a recursive approach that walks through each subdirectory and recreates the same structure in the destination.

Here’s a complete example in C#:

### Recursive Directory Copy (Preserving Structure)

```cs
using System;
using System.IO;

public class DirectoryCopier
{
    public static void CopyDirectory(string sourceDir, string destDir)
    {
        // Create the destination directory if it doesn't exist
        Directory.CreateDirectory(destDir);

        // Copy all files in the current directory
        foreach (var filePath in Directory.GetFiles(sourceDir))
        {
            string fileName = Path.GetFileName(filePath);
            string destFilePath = Path.Combine(destDir, fileName);
            File.Copy(filePath, destFilePath, overwrite: true);
        }

        // Recurse into subdirectories
        foreach (var subDir in Directory.GetDirectories(sourceDir))
        {
            string subDirName = Path.GetFileName(subDir);
            string newDestSubDir = Path.Combine(destDir, subDirName);
            CopyDirectory(subDir, newDestSubDir);
        }
    }
}
```

### Example Usage

```cs
string source = @"C:\SourceFolder";
string destination = @"D:\BackupFolder";

DirectoryCopier.CopyDirectory(source, destination);
```

### Notes

- This method recreates the exact folder and file hierarchy from the source in the destination.
- Files are overwritten if they already exist in the destination (`overwrite: true`).
- It works for both small and large directory trees.


---

Original Source: https://www.mindstick.com/forum/161603/how-do-you-copy-all-files-from-one-directory-to-another-preserving-the-structure

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
