Copy folder and exclude files by extension - CSharp System.IO

CSharp examples for System.IO:File Copy

Description

Copy folder and exclude files by extension

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;//  w  w w .  jav  a2 s .c  o  m
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Sample take from http://msdn.microsoft.com/en-us/library/bb762914.aspx
        /// </summary>
        /// <param name="sourceDirName"></param>
        /// <param name="destDirName"></param>
        /// <param name="copySubDirs"></param>
        public static void Copy(string sourceDirName, string destDirName, bool copySubDirs, params string[] excludedExtensions)
        {
            // Get the subdirectories for the specified directory.
            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 the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {                
                if (excludedExtensions != null && excludedExtensions.Length > 0)
                    if (excludedExtensions.Contains(file.Extension.ToLower()))
                        continue;
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {                    
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    Copy(subdir.FullName, temppath, copySubDirs, excludedExtensions);
                }
            }

        }
}

Related Tutorials