Copy Directory And Its Content To Another Directory In C#

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace SelectiveFileCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            //This program copies Temp2 folder under Temp1 in D:
            string sourceDir = @"C:\Temp2\";
            string destDir = @"D:\Temp1\";
            DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);
            string[] dirList = Directory.GetDirectories(sourceDir, "*", SearchOption.AllDirectories);
            string[] fileList = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            foreach (string str in dirList)
            {
                Directory.CreateDirectory(destDir + str.Substring(3));
            }
            foreach (string str in fileList)
            {
                File.Copy(str, destDir + str.Substring(3), true);
            }
            sw.Stop();
            Console.WriteLine(@"Total time (in seconds) taken for directory and file copy: " + sw.Elapsed.TotalSeconds);
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s