Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET Directory Copy Program

0.00/5 (No votes)
24 Jun 2005 1  
The article fills in the gap for the .NET framework to copy directory contents.

Introduction

This article submitted is my first posting in CodeProject. The code given along side is for the use of copying all the contents of a folder and if needed it can also again try to copy it some where else if the process failed.

Background

The Microsoft .NET framework does not provide for the functionality to copy a directory. This article and the code attached with it solves the particular problem.

Using the code

The heart of all is this function neatly searches the directory and the files and then copies them to a new directory after the definition table is created.

public void DirectorySearch(string strSourceDirectory, 
             string strSourceDirectoryConstant, 
             string strDestinationDirectory)
{
    iNoOfDir++;
    string [] strarrDirectoryNames= Directory.GetDirectories(strSourceDirectory);
    string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
    foreach(string strFileName in strarrFileNames)
    {
        try
        {
            iNoOfFiles++;
            FileInfo info=new FileInfo(strFileName);
            long fileLength= info.Length;
            totalLength =totalLength+fileLength;
            //Below the function Insert is used which 

            //is found in the code : purpose to store the data

            Insert("FILE", strFileName, strSourceDirectoryConstant + @"\" + 
                   info.Name, fileLength.ToString(), strDestinationDirectory);
            info=null;
        }
        catch
        {
        }
    }
    foreach(string strDirectoryName in strarrDirectoryNames)
    {
        DirectoryInfo info=new DirectoryInfo(strDirectoryName);
        DirectorySearch(strDirectoryName, strSourceDirectoryConstant 
                        + @"\" + info.Name, strDestinationDirectory);
        Insert("DIRECTORY", strDirectoryName, strSourceDirectoryConstant 
               + @"\" + info.Name, "", strDestinationDirectory);
        info=null;
    }
}

History

  • Version 1.0 - 5:19 PM 6/20/2005.

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