Access to the path 'c:\$Recycle.Bin\S-1-5-18' is denied

前端 未结 4 812
天涯浪人
天涯浪人 2020-12-31 22:05

I have this code to copy all files from source-directory, F:\\, to destination-directory.

public void Copy(string sourceDir, string targetDir)
{         


        
相关标签:
4条回答
  • 2020-12-31 22:37

    File system objects are subject to security. Some file system objects are secured in such a way that they can only be accessed by certain users. You are encountering a file to which the user executing the code does not have sufficient rights to access.

    The reason that you don't have access rights for this particular folder is to protect the security of the different users on the system. The folder in question is the recycle bin on that drive. And each different user has their own private recycle bin, that only they have permission to access. If anybody could access any other user's recycle bin, then users would be able to read each other's files, a clear violation of the system's security policy.

    Perhaps the simplest way around this is to skip hidden folders at the root level of the drive. That simple change would be enough to solve your problem because you surely don't want to copy recycle bins.

    0 讨论(0)
  • 2020-12-31 22:43

    Use following function instead of System.IO.Directory.GetFiles:

    IEnumerable<String> GetAllFiles(string path, string searchPattern)
        {
            return System.IO.Directory.EnumerateFiles(path, searchPattern).Union(
                System.IO.Directory.EnumerateDirectories(path).SelectMany(d =>
                {
                    try
                    {
                        return GetAllFiles(d,searchPattern);
                    }
                    catch (UnauthorizedAccessException e)
                    {
                        return Enumerable.Empty<String>();
                    }
                }));
        }
    
    0 讨论(0)
  • 2020-12-31 22:50

    This should do the trick:

    private IEnumerable<string> RecursiveFileSearch(string path, string pattern, ICollection<string> filePathCollector = null)
    {
        try
        {
            filePathCollector = filePathCollector ?? new LinkedList<string>();
    
            var matchingFilePaths = Directory.GetFiles(path, pattern);
    
            foreach(var matchingFile in matchingFilePaths)
            {
                filePathCollector.Add(matchingFile);
            }
    
            var subDirectories = Directory.EnumerateDirectories(path);
    
            foreach (var subDirectory in subDirectories)
            {
                RecursiveFileSearch(subDirectory, pattern, filePathCollector);
            }
    
            return filePathCollector;
        }
        catch (Exception error)
        {
            bool isIgnorableError = error is PathTooLongException ||
                error is UnauthorizedAccessException;
    
            if (isIgnorableError)
            {
                return Enumerable.Empty<string>();
            }
    
            throw error;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 22:51

    That folder is a secure system folder (your bin, each drive has its own bin). Just place your file.copy into a try catch statement and ignore/log all the failures. That way you will only copy actual files and skip system files/folders.

    If you really want to avoid the try catch statement. Use the fileinfo and directory info classes to figure out which folders/files are of the system and will throw an exception.

    0 讨论(0)
提交回复
热议问题