
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to Copy the entire contents of a directory in C#?
While copying the entire contents of directory, it is more important that we have copy its sub directories and the related files.
Example
Let us consider demo source directory having sub directories and files like below.
Below is the demo target directory which is empty initially.
using System; using System.IO; namespace DemoApplication { class Program { public static void Main() { string sourceDirectory = @"d:\DemoSourceDirectory"; string targetDirectory = @"d:\DemoTargetDirectory"; DirectoryInfo sourceDircetory = new DirectoryInfo(sourceDirectory); DirectoryInfo targetDircetory = new DirectoryInfo(targetDirectory); CopyAll(sourceDircetory, targetDircetory); Console.ReadLine(); } public static void CopyAll(DirectoryInfo source, DirectoryInfo target) { Directory.CreateDirectory(target.FullName); foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); } foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir); } } } }
Output
The output of the above code is
- Related Articles
- How can I list the contents of a directory in Python?
- Copy a directory to an existing directory Linux?
- How to copy files into a directory in C#?
- How to copy the entire ArrayList to a one-dimensional Array in C# ?
- Java program to merge contents of all the files in a directory
- Copy the entire LinkedList to Array in C#
- How to copy a file, group of files, or directory in Linux?
- How to image copy the entire DB2 table TAB1 into a dataset?
- How to copy files to a new directory using Python?
- How to copy folder contents in PowerShell with –Recurse parameter?
- C program to copy the contents of one file to another file?
- How are entire non-empty directory trees removed using Python?
- How to save the contents of a Textbox in Tkinter?
- How to print out the contents of a vector in C++?
- How to retrieve the contents of a text field in JavaFX?

Advertisements