[C#] - A faster .NET module to copy any directory without errors

Discussion in 'Mixed Languages' started by Josh Cell, Apr 23, 2012.

  1. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,105
    120
    #1 Josh Cell, Apr 23, 2012
    Last edited by a moderator: Apr 20, 2017
    We know; The native .NET file finder module have problems when found a system file protected by ACL, causing problems on the copy.

    This C# functions will correctly copy any directory with/without protected files:

    Code:
    private bool CopyDirectory(string SourcePath, string DestinationPath, bool Recursive)
            {
                List<string> Paths = new List<string>();
                List<string> Files = new List<string>();
                foreach (string StringPath in DirectoryList(SourcePath, Recursive, null))
                    Paths.Add(StringPath);
                foreach (string StringPath in FileList(SourcePath, Recursive, null))
                    Files.Add(StringPath);
                try
                {
                    foreach (string dirPath in Paths)
                        System.IO.Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
                    foreach (string newPath in Files)
                    {
                        System.IO.File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), Recursive);
                    }
                    return true;
                }
                catch { return false; }
            }
    
            private static List<string> FileList(string RootDirectory,
                bool SearchAllDirectories, Predicate<string> Filter)
            {
                List<string> retList = new List<string>();
    
                try
                {
                    List<string> DirList = new List<string> { RootDirectory };
    
                    if (SearchAllDirectories)
                        DirList.AddRange(DirectoryList(RootDirectory, SearchAllDirectories, Filter));
    
                    foreach (string DirectoryStr in DirList)
                    {
                        DirectoryInfo di = new DirectoryInfo(DirectoryStr);
                        try
                        {
                            foreach (FileInfo FileStr in di.EnumerateFiles())
                            {
                                try
                                {
                                    if ((Filter == null) || (Filter(FileStr.FullName)))
                                        retList.Add(FileStr.FullName);
                                }
    
                                catch (Exception excep) { }
                            }
                        }
                        catch (UnauthorizedAccessException) { }
                        catch (Exception) { }
                    }
                }
                catch (Exception) { }
                return retList;
            }
    
            private static List<string> DirectoryList(string RootDirectory,
                bool SearchAllDirectories, Predicate<string> Filter)
            {
                List<string> retList = new List<string>();
                try
                {
                    DirectoryInfo di = new DirectoryInfo(RootDirectory);
    
                    foreach (DirectoryInfo DirectoryStr in di.EnumerateDirectories())
                    {
                        try
                        {
                            if ((Filter == null) || (Filter(DirectoryStr.FullName)))
                            {
                                retList.Add(DirectoryStr.FullName);
    
                                if (SearchAllDirectories)
                                    retList.AddRange(DirectoryList(DirectoryStr.FullName, SearchAllDirectories,
                                        Filter));
                            }
                        }
                        catch (UnauthorizedAccessException) { }
    
                        catch (Exception) { }
                    }
                }
                catch (Exception) { }
                return retList;
            }
    Usage:
    Code:
    CopyDirectory(SourcePath, DestinationPath, RecursiveSearch);
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...