0 Replies - 1731 Views - Last Post: 11 April 2012 - 06:24 AM

#1 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1234
  • View blog
  • Posts: 1,760
  • Joined: 24-August 11

Copy a directory and its contents

Posted 11 April 2012 - 06:24 AM

Description: Either place in a class library, build, and reference the .dll (that's how I use it), or drop into a class in your project. Call CopyDir and pass the source path and the destination path respectively. I included a MessageBox to show any errors, but it can be removed because it returns a bool.Uses recursion to copy a directory and its contents (including subdirectories) to a specified location.
using System;
using System.IO;
using System.Windows.Forms;

public class CopyDirectory
{
    /// 
    /// Method to copy a directory, and all of its files and subdirectories.
    /// 
    /// String source directory path
    /// String target directory path
    /// Bool result
    public bool CopyDir(string srcPath, string tarPath)
    {
        var srcDir = new DirectoryInfo(srcPath);
        var tarDir = new DirectoryInfo(tarPath);

        try
        {
            FileCopy(srcDir, tarDir);
            DirCopy(srcDir, tarDir);
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show("The copy failed: " + e);
            return false;
        }
    }

    /// 
    /// Method to copy all files in a directory. Quietly exits if there are no files to copy. Deletes 
    /// any existing instance of file in destination.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private void FileCopy(DirectoryInfo srcDir, DirectoryInfo tarDir)
    {
        var files = srcDir.GetFiles();

        if (files.Length <= 0) return;
            
        foreach (var file in files)
        {
            var newTarPath = file.FullName.Replace(srcDir.FullName, tarDir.FullName);
            var newFile = new FileInfo(newTarPath);

            if(newFile.Exists)
                newFile.Delete();

            file.CopyTo(newFile.ToString());
        }
    }

    /// 
    /// Recursively copies the contents of a directory and all of its subdirectories. Quietly exits at 
    /// the bottom of a directory. Deletes any existing instance of a directory in the target location.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private void DirCopy(DirectoryInfo srcDir, DirectoryInfo tarDir)
    {
        var directories = srcDir.GetDirectories();

        if (directories.Length <= 0) return;

        foreach(var dir in directories)
        {
            var newTarPath = dir.FullName.Replace(srcDir.FullName, tarDir.FullName);
            var newTarDir = new DirectoryInfo(newTarPath);

            if(newTarDir.Exists)
                newTarDir.Delete(true);

            newTarDir.Create();
            FileCopy(dir, newTarDir);

            if(IsParent(dir))
            {
                DirCopy(dir, newTarDir);
            }
        }
    }

    /// 
    /// Checks if a directory has subdirectories
    /// 
    /// DirectoryInfo() to be checked
    /// True if directory has subdirectories
    private bool IsParent(DirectoryInfo dir)
    {
        var directories = dir.GetDirectories();
        return directories.Length > 0;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Copy a directory and its contents

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1234
  • View blog
  • Posts: 1,760
  • Joined: 24-August 11

Re: Copy a directory and its contents

Posted 11 April 2012 - 06:24 AM

Description: Either place in a class library, build, and reference the .dll (that's how I use it), or drop into a class in your project. Call CopyDir and pass the source path and the destination path respectively. I included a MessageBox to show any errors, but it can be removed because it returns a bool.Uses recursion to copy a directory and its contents (including subdirectories) to a specified location. EDIT: 5/2/12 - I've added functionality to copy ACLs for files and directories.
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Windows.Forms;

public class CopyDirectory
{
    /// 
    /// Method to copy a directory, and all of its files and subdirectories.
    /// 
    /// String source directory path
    /// String target directory path
    /// Bool result
    public static bool CopyDir(string srcPath, string destPath)
    {
        var srcDir = new DirectoryInfo(srcPath);
        var destDir = new DirectoryInfo(destPath);

        try
        {
            FileCopy(srcDir, destDir);
            DirCopy(srcDir, destDir);
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show("The copy failed: " + e);
            return false;
        }
    }

    /// 
    /// Method to copy all files in a directory. Quietly exits if there are no files to copy. Deletes 
    /// any existing instance of file in destination.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private static void FileCopy(DirectoryInfo srcDir, DirectoryInfo destDir)
    {
        var files = srcDir.GetFiles();

        if (files.Length <= 0) return;

        foreach (var file in files)
        {
            var destPath = file.FullName.Replace(srcDir.FullName, destDir.FullName);
            var newFile = new FileInfo(destPath);
                
            // Pass ACL from original file to new file
            var fSecurity = file.GetAccessControl();
            var accessRules = fSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier));
            var newFSecurity = new FileSecurity();
                
            foreach(var rule in accessRules)
            {
                newFSecurity.SetAccessRule((FileSystemAccessRule)rule);
            }
                
            file.CopyTo(newFile.FullName, true);
            newFile.SetAccessControl(newFSecurity);
        }
    }

    /// 
    /// Recursively copies the contents of a directory and all of its subdirectories. Quietly exits at 
    /// the bottom of a directory. Deletes any existing instance of a directory in the target location.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private static void DirCopy(DirectoryInfo srcDir, DirectoryInfo destDir)
    {
        var directories = srcDir.GetDirectories();

        if (directories.Length <= 0) return;

        foreach(var dir in directories)
        {
            var dirAcl = dir.GetAccessControl();

            var destPath = dir.FullName.Replace(srcDir.FullName, destDir.FullName);
            var newDir = new DirectoryInfo(destPath);

            if (newDir.Exists)
                newDir.Delete(true);

            newDir.Create(dirAcl);
            FileCopy(dir, newDir);

            if(IsParent(dir))
            {
                DirCopy(dir, newDir);
            }
        }
    }

    /// 
    /// Checks if a directory has subdirectories
    /// 
    /// DirectoryInfo() to be checked
    /// True if directory has subdirectories
    private static bool IsParent(DirectoryInfo dir)
    {
        var directories = dir.GetDirectories();
        return directories.Length > 0;
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1234
  • View blog
  • Posts: 1,760
  • Joined: 24-August 11

Re: Copy a directory and its contents

Posted 11 April 2012 - 06:24 AM

Description: Either place in a class library, build, and reference the .dll (that's how I use it), or drop into a class in your project. Call CopyDir and pass the source path and the destination path respectively. I included a MessageBox to show any errors, but it can be removed because it returns a bool.Uses recursion to copy a directory and its contents (including subdirectories) to a specified location. EDIT: 5/2/12 - I've added functionality to copy ACLs for files and directories.
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Windows.Forms;

public class CopyDirectory
{
    /// 
    /// Method to copy a directory, and all of its files and subdirectories.
    /// 
    /// String source directory path
    /// String target directory path
    /// Bool result
    public static bool CopyDir(string srcPath, string destPath)
    {
        var srcDir = new DirectoryInfo(srcPath);
        var destDir = new DirectoryInfo(destPath);

        if(!destDir.Exists)
        {
            destDir.Create();
        }

        try
        {
            FileCopy(srcDir, destDir);
            CopyContents(srcDir, destDir);
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show("The copy failed: " + e);
            return false;
        }
    }

    /// 
    /// Method to copy all files in a directory. Quietly exits if there are no files to copy. Deletes 
    /// any existing instance of file in destination.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private static void FileCopy(DirectoryInfo srcDir, DirectoryInfo destDir)
    {
        var files = srcDir.GetFiles();

        if (files.Length <= 0) return;

        foreach (var file in files)
        {
            var destPath = file.FullName.Replace(srcDir.FullName, destDir.FullName);
            var newFile = new FileInfo(destPath);
                
            // Pass ACL from original file to new file
            var fSecurity = file.GetAccessControl();
            var accessRules = fSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier));
            var newFSecurity = new FileSecurity();
                
            foreach(var rule in accessRules)
            {
                newFSecurity.SetAccessRule((FileSystemAccessRule)rule);
            }
                
            file.CopyTo(newFile.FullName, true);
            newFile.SetAccessControl(newFSecurity);
        }
    }

    /// 
    /// Recursively copies the contents of a directory and all of its subdirectories. Quietly exits at 
    /// the bottom of a directory. Deletes any existing instance of a directory in the target location.
    /// 
    /// Source DirectoryInfo()
    /// Target DirectoryInfo()
    private static void CopyContents(DirectoryInfo srcDir, DirectoryInfo destDir)
    {
        var directories = srcDir.GetDirectories();

        if (directories.Length <= 0) return;

        foreach(var dir in directories)
        {
            var dirAcl = dir.GetAccessControl();

            var destPath = dir.FullName.Replace(srcDir.FullName, destDir.FullName);
            var newDir = new DirectoryInfo(destPath);

            if (newDir.Exists)
                newDir.Delete(true);

            newDir.Create(dirAcl);
            FileCopy(dir, newDir);

            if(IsParent(dir))
            {
                CopyContents(dir, newDir);
            }
        }
    }

    /// 
    /// Checks if a directory has subdirectories
    /// 
    /// DirectoryInfo() to be checked
    /// True if directory has subdirectories
    private static bool IsParent(DirectoryInfo dir)
    {
        var directories = dir.GetDirectories();
        return directories.Length > 0;
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1