Click here to Skip to main content
Click here to Skip to main content

Function to copy a directory to another place (nothing fancy)

, 19 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Simple C#/.NET tip to copy an entire directory tree to another directory


I have been working with the .NET framework for several weeks now and I really enjoy the API. But sometimes I miss some features I need right now, even if I expect the framework to grow and get new classes and capabilities in the forthcoming versions (like Java did).

This article doesn't try to teach something but just gives a solution to anyone who needs it. I tried to keep it simple with few lines of code.

The FileSystem class

This class includes high level functions missing in the standard System.IO namespace. The class provided here only includes a directory to directory copy function for the moment, and the purpose of this article is to fix this .NET missing feature that many VB developers (for example) are used to.

The function takes two absolute paths (source directory and destination directory) as parameters and returns a boolean equal to true when the copy succeeds. Please note that this function automatically overwrites a destination file with the same name. Of course all subdirectories are also copied recursively.

using System;
using System.IO;

namespace Utility.IO{
    /// <summary>
    /// Filesystem
    /// </summary>
    public class FileSystem{
        // Copy directory structure recursively
        public static void copyDirectory(string Src,string Dst){
            String[] Files;

            if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
            foreach(string Element in Files){
                // Sub directories
                // Files in directory


An usage example

Here is an example of how to use the FileSystem class.

// After a successful copy, you can then call 
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
catch(Exception Ex){


This article is just a tip targeted to beginners or newcomers who noticed this missing feature in the .NET framework. It is provided as a possible solution, but I encourage anyone to write his own function.

Happy Coding !!!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior) Siliconz Ltd
New Zealand New Zealand
Richard Lopes
Just Programmer

Comments and Discussions

GeneralEnhance to get file filter, sample call Pinmemberli hai3-Feb-05 6:57 
QuestionHow about mapped drives? Pinsussotc4-Aug-04 8:51 
AnswerRe: How about mapped drives? Pinmemberbass4g0d23-Oct-08 7:41 
GeneralLittle help required ! Pinsussvikas123423-Mar-04 4:30 
GeneralVB.NET Version Pinmemberstarlogic8-Feb-04 14:56 
GeneralRe: VB.NET Version Pinmemberkorcutt27-Apr-06 12:28 
QuestionHow to determine drive types Pinmemberliuhoihing17-Nov-03 20:36 
AnswerRe: How to determine drive types Pinmemberchinese_zmm25-Mar-09 0:18 
you can use driveinfo instead Smile | :)
AnswerRe: How to determine drive types PinmemberFrozenHearted26-Mar-09 0:34 
QuestionWhy this way? PinmemberMustafa Demirhan19-Nov-02 16:54 
AnswerRe: Why this way? PinmemberGriffonRL19-Nov-02 22:35 
GeneralRe: Why this way? PinmemberMustafa Demirhan20-Nov-02 1:18 
GeneralRe: Why this way? PinmemberGriffonRL20-Nov-02 2:24 
GeneralRe: Why this way? PinmemberBarryJ21-Nov-02 6:09 
GeneralRe: Why this way? PinmemberGriffonRL21-Nov-02 6:45 
GeneralGreat article... PinmemberDavid Stone19-Nov-02 6:13 
GeneralRe: Great article... PinmemberGriffonRL19-Nov-02 6:42 
GeneralRe: Great article... PinmemberDavid Stone19-Nov-02 16:32 
GeneralRe: Great article... PinmemberMustafa Demirhan19-Nov-02 16:51 
GeneralRe: Great article... PinmemberGriffonRL19-Nov-02 22:21 
GeneralRe: Great article... PinmemberDavid Stone20-Nov-02 12:08 
GeneralRe: Great article... PinsussAnonymous20-Nov-02 22:42 
GeneralRe: Great article... PinmemberDavid Stone21-Nov-02 14:32 
GeneralRe: Great article... Pinmemberjalbitz12-Nov-03 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 20 Nov 2002
Article Copyright 2002 by GriffonRL
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid