The System.IO.DirectoryInfo class does not come with a method to copy a directory. In this article, we’ll see how to create a method to do that, and then use it in an ASP.NET page.

The ASPX web form interface

Before you try this, make sure that you have write access on your web server. Typically, the anonymous user, which is used to access a website, is not allowed such permissions, but will need them to be able to copy folders from one location to another. If you do not want to give write permissions to everybody on your site, then create a new user, grant them the proper permissions, and log in with that account instead.

To begin with, let’s create an ASPX web form where we can test our code.

The result of the above HTML source code is this:

Our web form contains 2 textboxes, one button and one hidden label web control (to show status messages after clicking on the button). Our button calls a Copy_Click method when clicked, which is responsible for parsing out the source and destination paths, and in turn call a CopyDirectory method, both of which will be shown below.

Adding the Copy_Click method

As we saw before, the Copy_Click method is called when we click on the button on our web page. It uses the RegEx.Split() method to convert the source directory into an array, based on the “/” characters in the path. We do this so that we can get the last folder name of the source path, and then append that to the destination path.  For example:

Using our parsing, the destination directory will therefore become /test/webmaster/evagoras.  Once we have both our source and correct destination folders, we convert them into physical paths, using the Server.MapPath() method. A physical path is one which gives the complete path of a file/folder in the system, for example C:\inetpub\wwwroot\images\xefteri.gif.  We pass the source and destination paths to the CopyDirectory method, which is nested inside a Try…Catch statement to catch any possible exceptions. Any exceptions are shown through our label web control, lblStatusMessage, on the page.

The CopyDirectory method

A typical method of this kind would include 3 parameters:

  1. Source directory to copy from. Let’s make this a String and call it sourcePath.
  2. Destination directory to copy to. Let’s make this a String again and call it destPath.
  3. Whether to overwrite any files with the same name at the destination directory or not. Let’s make this a Boolean variable and call it overwrite.

Let’s first take a look at the complete code of the method, and then we’ll go through it in more detail.

If the source directory is not a valid one, then it doesn’t make sense to do anything. Therefore, we wrap all our code inside an IF statement – only proceed if the source folder exists. Then we check if the destination folder exists or not, and if it doesn’t we create it by using the Create() method. We use the GetFiles() method of the System.IO.DirectoryInfo class to loop through all the files inside the source directory. For each file that we find, we copy it to the destination folder if it’s not there, or if we chose to overwrite everything (despite if it’s there or not). The path to copy the files to, is easily derived by using the System.IO.Path.Combine() method, which joins the folder path with the file name for us.

Then we use the GetDirectories() method of the class to loop through all the subfolders of our source folder, and for each one we find, we call our method recursively. A successful copy would result in a screen like so:

While an unsuccessful attempt, due to a source directory not existing, would result in a screen like so:

We have seen how to copy a directory with ASP.NET, something that is not provided in the extensive library of methods of the .NET framework.  You could use this method for example, in an online file manager to select one folder and copy it to another location.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.