Click here to Skip to main content
15,890,123 members
Articles / Programming Languages / C#
Article

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 407.3K   2.7K   61   55
Simple C#/.NET tip to copy an entire directory tree to another directory

Introduction

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.

C#
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(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar) 
                Dst+=Path.DirectorySeparatorChar;
            if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
            Files=Directory.GetFileSystemEntries(Src);
            foreach(string Element in Files){
                // Sub directories
                if(Directory.Exists(Element)) 
                    copyDirectory(Element,Dst+Path.GetFileName(Element));
                // Files in directory
                else 
                    File.Copy(Element,Dst+Path.GetFileName(Element),true);
                }
            }

        }
    }

An usage example

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

C#
// After a successful copy, you can then call 
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
try{
    copyDirectory(@"c:\MySrcDirectory",@"c:\MyDstDirectory");
    }
catch(Exception Ex){
    Console.Error.WriteLine(Ex.Message);
    }

Conclusion

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 !!!

License

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


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

Comments and Discussions

 
GeneralRe: VB.NET Version Pin
korcutt27-Apr-06 11:28
korcutt27-Apr-06 11:28 
QuestionHow to determine drive types Pin
Hing17-Nov-03 19:36
Hing17-Nov-03 19:36 
AnswerRe: How to determine drive types Pin
chinese_zmm24-Mar-09 23:18
chinese_zmm24-Mar-09 23:18 
AnswerRe: How to determine drive types Pin
FrozenHearted25-Mar-09 23:34
FrozenHearted25-Mar-09 23:34 
QuestionWhy this way? Pin
Mustafa Demirhan19-Nov-02 15:54
Mustafa Demirhan19-Nov-02 15:54 
AnswerRe: Why this way? Pin
GriffonRL19-Nov-02 21:35
GriffonRL19-Nov-02 21:35 
GeneralRe: Why this way? Pin
Mustafa Demirhan20-Nov-02 0:18
Mustafa Demirhan20-Nov-02 0:18 
GeneralRe: Why this way? Pin
GriffonRL20-Nov-02 1:24
GriffonRL20-Nov-02 1:24 
Hello,

Yes I agree that C# is a very nice language and we will soon get new important features like generics Big Grin | :-D .
I have a long experience with Java and C++, and I found C# very easy to learn with that background.
I first played a little bit with ASP.NET 1 year ago but stopped for several months. I resumed my experiments a few months ago with the Visual Studio .NET release, and now we use .NET to build our business applications.
I really think this language is more productive than C++, or at least less harmful than C++. Writing bad C++ code is easier than writing bad C# code. Of course C# still miss some features but is is coming soon.
I have nothing against the .NET framework and it is full of helpful classes that speed up the development process a lot. I programmed a lot with Java and really enjoyed the Java APIs. The new .NET framework makes more sense than the system DLL we used to for Windows programming. MFC for example has a heavy heritage behind it.
The beauty of .NET if you develop only for Windows platforms, is that you can still write critical functions in a native C++ DLL and call them from your skeleton program written in C#/.NET.
Because the C# language has been submitted to the ECMA and specifications are available, anybody could write a C# compiler to native code. Or maybe someone will create a program to convert all CLR code into native code: some solutions exist for Java where .class files are converted into Windows .EXE.

So wait and see,

R. LOPES
Just programmer.
GeneralRe: Why this way? Pin
BarryJ21-Nov-02 5:09
BarryJ21-Nov-02 5:09 
GeneralRe: Why this way? Pin
GriffonRL21-Nov-02 5:45
GriffonRL21-Nov-02 5:45 
GeneralGreat article... Pin
David Stone19-Nov-02 5:13
sitebuilderDavid Stone19-Nov-02 5:13 
GeneralRe: Great article... Pin
GriffonRL19-Nov-02 5:42
GriffonRL19-Nov-02 5:42 
GeneralRe: Great article... Pin
David Stone19-Nov-02 15:32
sitebuilderDavid Stone19-Nov-02 15:32 
GeneralRe: Great article... Pin
Mustafa Demirhan19-Nov-02 15:51
Mustafa Demirhan19-Nov-02 15:51 
GeneralRe: Great article... Pin
GriffonRL19-Nov-02 21:21
GriffonRL19-Nov-02 21:21 
GeneralRe: Great article... Pin
David Stone20-Nov-02 11:08
sitebuilderDavid Stone20-Nov-02 11:08 
GeneralRe: Great article... Pin
Anonymous20-Nov-02 21:42
Anonymous20-Nov-02 21:42 
GeneralRe: Great article... Pin
David Stone21-Nov-02 13:32
sitebuilderDavid Stone21-Nov-02 13:32 
GeneralRe: Great article... Pin
jalbitz12-Nov-03 8:11
jalbitz12-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.