Copy folder recursive - CSharp File IO

CSharp examples for File IO:Directory

Description

Copy folder recursive

Demo Code


using System.IO;//from   w  w w  .  j av  a 2s  . c o  m
using System;

public class Main{
    public static void Copy(string sourceDirName, string destDirName, bool recursive, bool overwrite)
      {
         DirectoryInfo dir = new DirectoryInfo(sourceDirName);
         DirectoryInfo[] dirs = dir.GetDirectories();

         if (!dir.Exists)
         {
            throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
         }

         if (!Directory.Exists(destDirName))
         {
            Directory.CreateDirectory(destDirName);
         }

         FileInfo[] files = dir.GetFiles();

         foreach (FileInfo file in files)
         {
            file.CopyTo(Path.Combine(destDirName, file.Name), overwrite);
         }

         if (recursive)
         {
            foreach (DirectoryInfo subdir in dirs)
            {
               Copy(subdir.FullName, Path.Combine(destDirName, subdir.Name), recursive, overwrite);
            }
         }
      }
}

Related Tutorials