Click here to Skip to main content
13,145,668 members (34,067 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 18 Nov 2002

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

You may also be interested in...

Comments and Discussions

AnswerRe: How to determine drive types Pin
FrozenHearted25-Mar-09 23:34
memberFrozenHearted25-Mar-09 23:34 
QuestionWhy this way? Pin
Mustafa Demirhan19-Nov-02 15:54
memberMustafa Demirhan19-Nov-02 15:54 
AnswerRe: Why this way? Pin
GriffonRL19-Nov-02 21:35
memberGriffonRL19-Nov-02 21:35 
GeneralRe: Why this way? Pin
Mustafa Demirhan20-Nov-02 0:18
memberMustafa Demirhan20-Nov-02 0:18 
GeneralRe: Why this way? Pin
GriffonRL20-Nov-02 1:24
memberGriffonRL20-Nov-02 1:24 
GeneralRe: Why this way? Pin
BarryJ21-Nov-02 5:09
memberBarryJ21-Nov-02 5:09 
GeneralRe: Why this way? Pin
GriffonRL21-Nov-02 5:45
memberGriffonRL21-Nov-02 5:45 
GeneralGreat article... Pin
David Stone19-Nov-02 5:13
memberDavid Stone19-Nov-02 5:13 
First off, it's a great article that points out another thing that's wrong with the FCL. Now...if you don't mind, I'd like to critique it a bit. Big Grin | :-D

First off, there's no reason to return bool. A better way to do it would be to use straight Exceptions. To put it in Rama's words: "It's not the way of the Framework." The System.IO.File.Copy() method is marked as public static void Copy(), so what you should do is make this return void and stick to using Exceptions.

Another thing, when you say:
catch(Exception Ex)

That's wrong. Your code, as part of a class library, should never visually display information...and that includes writing to the console. You should let the consumer handle that. Jeffrey Richter points this out in his book Applied Microsoft .NET Framework Programming, the DataGrid displays a MessageBox when there is an error in setting the CurrentCell property. I know that as an app-writer, I would hate to have a component or utility that I was using display visual information to the user without my consent.

Oh, and you don't need a constructor in your class unless there are other methods that are not marked as static. So I would remove that because really, this class should never be instantiated.

That being said, it's a great implementation for something that should be in the Framework. Smile | :)

You will now find yourself in a wonderous, magical place, filled with talking gnomes, mythical squirrels, and, almost as an afterthought, your bookmarks
-Shog9 teaching Mel Feik how to bookmark

I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder

GeneralRe: Great article... Pin
GriffonRL19-Nov-02 5:42
memberGriffonRL19-Nov-02 5:42 
GeneralRe: Great article... Pin
David Stone19-Nov-02 15:32
memberDavid Stone19-Nov-02 15:32 
GeneralRe: Great article... Pin
Mustafa Demirhan19-Nov-02 15:51
memberMustafa Demirhan19-Nov-02 15:51 
GeneralRe: Great article... Pin
GriffonRL19-Nov-02 21:21
memberGriffonRL19-Nov-02 21:21 
GeneralRe: Great article... Pin
David Stone20-Nov-02 11:08
memberDavid Stone20-Nov-02 11:08 
GeneralRe: Great article... Pin
Anonymous20-Nov-02 21:42
sussAnonymous20-Nov-02 21:42 
GeneralRe: Great article... Pin
David Stone21-Nov-02 13:32
memberDavid Stone21-Nov-02 13:32 
GeneralRe: Great article... Pin
jalbitz12-Nov-03 8:11
memberjalbitz12-Nov-03 8:11 

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

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

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