Live with Dot Net Just another Programming weblog

Copy Directory from the file system

Posted on August 5, 2008

There is a missing class in the .net library to copy directory from the file system in the System.io namespace. The below code provide the functionality to copy directory to other location.
It needs two parameters source folders and the destination path.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
            }
        }

About admin

No description. Please complete your profile.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.