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

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

By , 19 Nov 2002
 

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.

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.

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

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
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 
GeneralPlease use C# PinmemberJasper4C#20-Jul-09 2:41 
GeneralSmall fix PinmemberMember 404930514-Jul-08 22:50 
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 
GeneralGood Article PinmemberKingTermite20-Jun-05 10:05 
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 
GeneralEnhance to get file filter, sample call Pinmemberli hai3-Feb-05 5:57 
QuestionHow about mapped drives? Pinsussotc4-Aug-04 7:51 
GeneralLittle help required ! Pinsussvikas123423-Mar-04 3:30 
GeneralVB.NET Version Pinmemberstarlogic8-Feb-04 13:56 
QuestionHow to determine drive types Pinmemberliuhoihing17-Nov-03 19:36 
QuestionWhy this way? PinmemberMustafa Demirhan19-Nov-02 15:54 
GeneralGreat article... PinmemberDavid Stone19-Nov-02 5:13 

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

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