Duality.PathHelper.CopyDirectory C# (CSharp) Method

CopyDirectory() public static method

Deep-Copies the source directory to the target path.
public static CopyDirectory ( string sourcePath, string targetPath, bool overwrite = false, Predicate filter = null ) : bool
sourcePath string
targetPath string
overwrite bool
filter Predicate
return bool
        public static bool CopyDirectory(string sourcePath, string targetPath, bool overwrite = false, Predicate<string> filter = null)
        {
            if (!Directory.Exists(sourcePath)) return false;
            if (!overwrite && Directory.Exists(targetPath)) return false;

            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);

            foreach (string file in Directory.GetFiles(sourcePath))
            {
                if (filter != null && !filter(file)) continue;
                File.Copy(file, Path.Combine(targetPath, Path.GetFileName(file)), overwrite);
            }
            foreach (string subDir in Directory.GetDirectories(sourcePath))
            {
                if (filter != null && !filter(subDir)) continue;
                CopyDirectory(subDir, Path.Combine(targetPath, Path.GetFileName(subDir)), overwrite, filter);
            }

            return true;
        }