How to copy a folder with all of its subfolders and files using Microsoft .NET Framework

By Mohammad Mahdi Ramezanpour at November 05, 2008 21:22
Filed Under: .NET General

It was about 1 month I didn’t post any blog because of my situation. I was bloody busy with 2 of my projects. Yesterday I passed one of them so I have more free time. Today I want to keep my blogs up with a new blog post about how you can copy a folder with all of its subfolders and files using .NET Framework.

I used Microsoft Visual Basic 2008 for this post. You can convert to C# using offline/online tools. I recommend DeveloperFusion Convertor.

We have 2 main sections:

  1. Copy Files.
  2. Copy folders.

Copy Files:

For each of our folders, we need to copy its files. So we need to write a method that copies all files contains in a folder. I use “CopyFiles” as a name for my method:

Private Sub CopyFiles(ByVal sourcePath As String, ByVal desPath As String)
End Sub

As you can see, my method has two input parameters:

  1. SourcePath: which gives my method the path of a folder I want to copy files from.
  2. desPath: which tells my method where to copy files.

Because this method wants to work on a directory, I have to declare a new variable of System.IO.DirectoryInfo:

Dim d As New System.IO.DirectoryInfo(sourcePath)

DirectoryInfo requires a parameter which takes the path of your specific directory.

After that, I need to search for file with a For Each statement. DirectoryInfo has a method named GetFiles() that gives an array of System.IO.FileInfo:

For Each fi As System.IO.FileInfo In d.GetFiles()
Next

In my For Each statement, I used some checking such as file existence and more; but the only thing that’s important is to copy files:

For Each fi As System.IO.FileInfo In d.GetFiles()
            If Not System.IO.File.Exists(System.IO.Path.Combine(d.FullName, fi.Name)) Then
                System.IO.File.Copy(d.FullName & "\" & fi.Name, desPath & "\" & fi.Name)
            Else
                System.IO.File.Delete(desPath & "\" & fi.Name)
                System.IO.File.Copy(d.FullName & "\" & fi.Name, desPath & "\" & fi.Name)
            End If
 Next

OK, now you can copy files but, what about folders? What about files in folders?

Copy Folders:

Public Sub CopyFolders(ByVal sourcePath As String, ByVal desPath As String)
    Dim d As New System.IO.DirectoryInfo(sourcePath)

Again, a new instance of System.IO.DirectoryInfo but we want to search for folders this time:

For Each di As System.IO.DirectoryInfo In d.GetDirectories()
Next

As you can see, I used GetDirectory() method this time because it will gives us an array of System.IO.DirectoryInfo of our main folder. Again some checking and the important part is the use of System.IO.Directory.CreateDirectory() method which makes a new directory for us:

For Each di As System.IO.DirectoryInfo In d.GetDirectories()
            If Not System.IO.Directory.Exists(desPath & "\" & di.Name) Then
                System.IO.Directory.CreateDirectory(desPath & "\" & di.Name)
            Else
                System.IO.Directory.Delete(desPath & "\" & di.Name, True)
                System.IO.Directory.CreateDirectory(desPath & "\" & di.Name)
            End If
            CopyFiles(di.FullName, desPath & "\" & di.Name)
            If di.GetDirectories().Length > 0 Then
                CopyFolders(di.FullName, desPath & "\" & di.Name)
            End If
Next

Pay attention that we want to look folders up recursively. So I called my “CopyFolders” method again in it. Another important section is that, I called “CopyFiles” method. It means, for each Folder, copy all of its files.

Download full source code based on WPF here: Click Here

Comments are closed

Currently Reading

Quote of the day

Send Persian SMS