Click here to Skip to main content
15,126,317 members
Articles / Programming Languages / C#
Posted 18 Nov 2002


61 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.42/5 (49 votes)
19 Nov 20021 min read
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

QuestionSo Elegantttttt.... Pin
shiwanijaiswal9-Mar-14 20:21
Membershiwanijaiswal9-Mar-14 20:21 
QuestionSweet! Pin
Member 103752903-Jan-14 13:30
professionalMember 103752903-Jan-14 13:30 
GeneralThank You Pin
Member 996046223-May-13 14:12
MemberMember 996046223-May-13 14:12 
GeneralThank You Pin
Member 996046223-May-13 14:09
MemberMember 996046223-May-13 14:09 
GeneralMy vote of 5 Pin
susindhran19-Jun-12 10:03
Membersusindhran19-Jun-12 10:03 
QuestionAll is well Pin
Krishnachytanya Ayyagari12-Dec-11 2:51
MemberKrishnachytanya Ayyagari12-Dec-11 2:51 
GeneralMy vote of 2 Pin
euggg1234567821-Jul-11 1:27
Membereuggg1234567821-Jul-11 1:27 
GeneralGood Article !!!!!!! :-) Pin
Suchitass11-May-11 2:02
MemberSuchitass11-May-11 2:02 
GeneralMy vote of 5 Pin
Murat TARKUN7-Nov-10 8:51
MemberMurat TARKUN7-Nov-10 8:51 
GeneralMicrosoft.VisualBasic.FileIO.FileSystem.CopyDirectory Pin
Vercas16-Oct-10 11:02
MemberVercas16-Oct-10 11:02 
Generalthanks for saving peoples time. Pin
Behrooz_cs22-Apr-10 22:06
MemberBehrooz_cs22-Apr-10 22:06 
Generalan easier way, non recursive Pin
kalvarez216-Dec-09 13:59
Memberkalvarez216-Dec-09 13:59 
GeneralRe: an easier way, non recursive Pin
drweb8630-May-11 22:50
Memberdrweb8630-May-11 22:50 
GeneralPlease use C# Pin
Jasper4C#20-Jul-09 3:41
MemberJasper4C#20-Jul-09 3:41 
GeneralRe: Please use C# Pin
Member 128588620-Feb-11 13:49
MemberMember 128588620-Feb-11 13:49 
GeneralSmall fix Pin
L€FF€14-Jul-08 23:50
MemberL€FF€14-Jul-08 23:50 
GeneralRe: Small fix Pin
chinese_zmm25-Mar-09 0:27
Memberchinese_zmm25-Mar-09 0:27 
GeneralMissing error handling Pin
Tzipi Levy30-Apr-08 1:44
MemberTzipi Levy30-Apr-08 1:44 
GeneralThank you Pin
ubshreenath24-Sep-07 9:44
Memberubshreenath24-Sep-07 9:44 
GeneralOdd Things Pin
Spencerja7-Sep-07 5:13
MemberSpencerja7-Sep-07 5:13 
GeneralExclude file Pin
GC9N6-Jun-07 5:34
MemberGC9N6-Jun-07 5:34 
GeneralThank you! Pin
kabbykray3-Jun-07 19:29
Memberkabbykray3-Jun-07 19:29 
GeneralSimpler one, based on string only Pin
nantcom24-Dec-06 6:22
Membernantcom24-Dec-06 6:22 
GeneralRe: Simpler one, based on string only Pin
Wernight8-Jul-08 23:55
MemberWernight8-Jul-08 23:55 
GeneralGood Article Pin
KingTermite20-Jun-05 11:05
MemberKingTermite20-Jun-05 11:05 

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.