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

QuestionSo Elegantttttt.... Pinmembershiwanijaiswal9-Mar-14 19:21 
QuestionSweet! PinprofessionalMember 103752903-Jan-14 12:30 
GeneralThank You PinmemberMember 996046223-May-13 13:12 
GeneralThank You PinmemberMember 996046223-May-13 13:09 
GeneralMy vote of 5 Pinmembersusindhran19-Jun-12 9:03 
QuestionAll is well PinmemberKrishnachytanya Ayyagari12-Dec-11 1:51 
GeneralMy vote of 2 Pinmembereuggg1234567821-Jul-11 0:27 
GeneralGood Article !!!!!!! :-) PinmemberSuchitass11-May-11 1:02 
GeneralMy vote of 5 PinmemberMurat TARKUN7-Nov-10 7:51 
GeneralMicrosoft.VisualBasic.FileIO.FileSystem.CopyDirectory PinmemberVercas16-Oct-10 10:02 
Generalthanks for saving peoples time. PinmemberBehrooz_cs22-Apr-10 21:06 
Generalan easier way, non recursive Pinmemberkalvarez216-Dec-09 12:59 
GeneralRe: an easier way, non recursive Pinmemberdrweb8630-May-11 21:50 
GeneralPlease use C# PinmemberJasper4C#20-Jul-09 2:41 
GeneralRe: Please use C# PinmemberMember 128588620-Feb-11 12:49 
GeneralSmall fix PinmemberMember 404930514-Jul-08 22:50 
GeneralRe: Small fix Pinmemberchinese_zmm24-Mar-09 23:27 
GeneralMissing error handling PinmemberTzipi Levy30-Apr-08 0:44 
GeneralThank you Pinmemberubshreenath24-Sep-07 8:44 
GeneralOdd Things PinmemberSpencerja7-Sep-07 4:13 
GeneralExclude file PinmemberGC9N6-Jun-07 4:34 
GeneralThank you! Pinmemberkabbykray3-Jun-07 18:29 
GeneralSimpler one, based on string only Pinmembernantcom24-Dec-06 5:22 
GeneralRe: Simpler one, based on string only PinmemberWernight8-Jul-08 22:55 
GeneralGood Article PinmemberKingTermite20-Jun-05 10:05 
GeneralRe: Good Article PinmemberGriffonRL20-Jun-05 21:10 
GeneralEnhance to get file filter, sample call //copyDirectory(@&quot;c:\temp&quot;,@&quot;c:\temp\new&quot;, &quot;*e.exe;*l.xml&quot;); Pinmemberli hai3-Feb-05 5:57 
GeneralRe: Enhance to get file filter, sample call //copyDirectory(@&quot;c:\temp&quot;,@&quot;c:\temp\new&quot;, &quot;*e.exe;*l.xml&quot;); PinmemberGriffonRL20-Jun-05 21:11 
GeneralRe: Enhance to get file filter, sample call //copyDirectory(@&quot;c:\temp&quot;,@&quot;c:\temp\new&quot;, &quot;*e.exe;*l.xml&quot;); PinmemberSteed Soft9-May-06 23:04 
GeneralRe: Enhance to get file filter, sample call //copyDirectory(@&quot;c:\temp&quot;,@&quot;c:\temp\new&quot;, &quot;*e.exe;*l.xml&quot;); Pinmembertirumal123128-Dec-06 19:27 
AnswerRe: Enhance to get file filter, sample call //copyDirectory(@&amp;amp;quot;c:\temp&amp;amp;quot;,@&amp;amp;quot;c:\temp\new&amp;amp;quot;, &amp;amp;quot;*e.exe;*l.xml&amp;amp;quot;); PinmemberSteed Soft28-Dec-06 21:16 
GeneralEnhance to get file filter, sample call Pinmemberli hai3-Feb-05 5:57 
QuestionHow about mapped drives? Pinsussotc4-Aug-04 7:51 
AnswerRe: How about mapped drives? Pinmemberbass4g0d23-Oct-08 6:41 
GeneralLittle help required ! Pinsussvikas123423-Mar-04 3:30 
GeneralVB.NET Version Pinmemberstarlogic8-Feb-04 13:56 
GeneralRe: VB.NET Version Pinmemberkorcutt27-Apr-06 11:28 
QuestionHow to determine drive types Pinmemberliuhoihing17-Nov-03 19:36 
AnswerRe: How to determine drive types Pinmemberchinese_zmm24-Mar-09 23:18 
AnswerRe: How to determine drive types PinmemberFrozenHearted25-Mar-09 23:34 
QuestionWhy this way? PinmemberMustafa Demirhan19-Nov-02 15:54 
AnswerRe: Why this way? PinmemberGriffonRL19-Nov-02 21:35 
GeneralRe: Why this way? PinmemberMustafa Demirhan20-Nov-02 0:18 
Thanks for the answer.
GriffonRL wrote:
Great question because I saw someone showing this solution in a newsgroup .
Hehe. Believe me this is my own question. I did not see it in a newsgroup Roll eyes | :rolleyes:
I have been using my approach for years and never had a problem yet.
GriffonRL wrote:
- Your code is not 100% .NET compliant.
- Your code is not portable to another framework version (like mono on Linux) because you rely on Windows commands.

Yeah you are right. I never thought that because framework compatibility or OS independency is never an issue for me. I do not care about that :P
GriffonRL wrote:
- No exceptions will be raised in case of an error occurring during the shell copy.
Yes but you will get a return value which indicates if the operation is sucessfull or not. Isn't it good enough? Hmmm. Well in fact not. I admit that C# is a very very nice language and the one who uses C# should use it in C# way, not the old C way. So let me answer my question: NO. Using Exceptions is better. Unsure | :~
Hey, but you can check the value and raise your own exception OMG | :OMG:
GriffonRL wrote:
- You start a new external process.
I really didn't know that. Are you sure about that? Confused | :confused:
PS: One nice thing with using Shell functions is that you can put the files directly to the Recycle Bin (instead of deleting them).
Lastly, let me ask you another question: How is your experience with .NET till now? Is it really more productive than VC++? I love C#; but I do not like the .NET Framework it self and I hate the CLR (or whatever it is, the intermediate compiler thing - the code is not compiled into the native code).
I hope someday, Microsoft will come up with a native C# compiler with a new version of MFC (I dont want any language interoperability. All I want is to use a language like C# not C++ Wink | ;) ).
Mustafa Demirhan
Sonork ID 100.9935:zoltrix

They say I'm lazy but it takes all my time
GeneralRe: Why this way? PinmemberGriffonRL20-Nov-02 1:24 
GeneralRe: Why this way? PinmemberBarryJ21-Nov-02 5:09 
GeneralRe: Why this way? PinmemberGriffonRL21-Nov-02 5:45 
GeneralGreat article... PinmemberDavid Stone19-Nov-02 5:13 
GeneralRe: Great article... PinmemberGriffonRL19-Nov-02 5:42 
GeneralRe: Great article... PinmemberDavid Stone19-Nov-02 15:32 
GeneralRe: Great article... PinmemberMustafa Demirhan19-Nov-02 15:51 

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 | Mobile
Web01 | 2.8.141015.1 | Last Updated 20 Nov 2002
Article Copyright 2002 by GriffonRL
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid