Loading ...

C#: Copying all directories of a folder to backup folder inside it | CodeAsp.Net

C#: Copying all directories of a folder to backup folder inside it

 /5
0 (0votes)

Below is the code which can help you to copy all the folders and files of a directory to a backup folder inside it. Let's create a helper class to do the same:

#region Using Directives

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

#endregion


    public static class FileHelper
    {

        public static void BackupDirectoryTo(this DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory)
        {
            if (!sourceDirectory.Exists)
            {
                return;
            }

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

            if (sourceDirectory == targetDirectory)
            {
                return;
            }

            //Get all the files from source directory and copy them to target directory
            FileInfo[] files = sourceDirectory.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(targetDirectory.FullName, file.Name), true);

            }

            //Get all the directories from source directory and copy them to target directory
            //with all the files contained in them recursively
            DirectoryInfo[] directories = sourceDirectory.GetDirectories();
            List<DirectoryInfo> filteredDirectories = new List<DirectoryInfo>();
            foreach (DirectoryInfo d in directories)
            {
                if (d.ToString() != targetDirectory.Name.ToString())
                {
                    filteredDirectories.Add(d);
                }
            }
            //List<DirectoryInfo> filteredDirectories = directories.Where(d => d.ToString() != targetDirectory.ToString()).ToList();

            foreach (DirectoryInfo directory in filteredDirectories)
            {
                // Get destination directory.
                string destinationDirectory = Path.Combine(targetDirectory.FullName, directory.Name);

                // Call CopyDirectoryTo() recursively.
                CopyDirectoryTo(directory, new DirectoryInfo(destinationDirectory));

            }
        }

        public static void CopyDirectoryTo(this DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory)
        {
            if (!sourceDirectory.Exists)
            {
                return;
            }

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

            //Get all the files from source directory and copy them to target directory
            FileInfo[] files = sourceDirectory.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(targetDirectory.FullName, file.Name), true);

            }

            //Get all the directories from source directory and copy them to target directory
            //with all the files contained in them recursively
            DirectoryInfo[] directories = sourceDirectory.GetDirectories();

            foreach (DirectoryInfo directory in directories)
            {
                // Get destination directory.
                string destinationDirectory = Path.Combine(targetDirectory.FullName, directory.Name);

                // Call CopyDirectoryTo() recursively.
                CopyDirectoryTo(directory, new DirectoryInfo(destinationDirectory));
            }
        }


    }//end class

Usage:

        string sourcePath = Server.MapPath("~/");
        var sourceDirectory = new DirectoryInfo(sourcePath);
        var targetDirectory = new DirectoryInfo(System.IO.Path.Combine(sourcePath, "Backup"));
        
        sourceDirectory.BackupDirectoryTo(targetDirectory);

Comments (no comments yet)

Top Posts