How to handle UnauthorizedAccessException when attempting to add files from location without permissions

前端 未结 2 1835
清酒与你
清酒与你 2020-12-28 09:01

I am trying to get all files from folder this way:

try
{
    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, \"*.*\", SearchOption.All         


        
相关标签:
2条回答
  • 2020-12-28 09:14

    see SafeFileEnumerator on this other post. I have used the SafeFileEnumerator code in the past with success. It prevents loosing the entire enumeration when you simply don't have access to a single file so you are still able to iterate through the files that you can access.

    EDIT: The version I have is slightly different from the one I linked to so let me share the version I have.

    public static class SafeFileEnumerator
    {
        public static IEnumerable<string> EnumerateDirectories(string parentDirectory, string searchPattern, SearchOption searchOpt)
        {
            try
            {
                var directories = Enumerable.Empty<string>();
                if (searchOpt == SearchOption.AllDirectories)
                {
                    directories = Directory.EnumerateDirectories(parentDirectory)
                        .SelectMany(x => EnumerateDirectories(x, searchPattern, searchOpt));
                }
                return directories.Concat(Directory.EnumerateDirectories(parentDirectory, searchPattern));
            }
            catch (UnauthorizedAccessException ex)
            {
                return Enumerable.Empty<string>();
            }
        }
    
        public static IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOpt)
        {
            try
            {
                var dirFiles = Enumerable.Empty<string>();
                if (searchOpt == SearchOption.AllDirectories)
                {
                    dirFiles = Directory.EnumerateDirectories(path)
                                        .SelectMany(x => EnumerateFiles(x, searchPattern, searchOpt));
                }
                return dirFiles.Concat(Directory.EnumerateFiles(path, searchPattern));
            }
            catch (UnauthorizedAccessException ex)
            {
                return Enumerable.Empty<string>();
            }
        }
    }
    

    Example Usage:

    foreach(string fileName in SafeFileEnumerator.EnumerateFiles(folderPath, "*" + extension, SearchOption.AllDirectories))
    {
        //Do something with filename, store into an array or whatever you want to do.
    }
    
    0 讨论(0)
  • 2020-12-28 09:18

    You can use FileSystemInfo objects and recursion to accomplish this:

    static List<string> files = new List<string>();
    
    static void MyMethod() {
        DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
        ProcessFolder(dir.GetFileSystemInfos());
    }
    
    static void ProcessFolder(IEnumerable<FileSystemInfo> fsi) {
        foreach (FileSystemInfo info in fsi) {
    
            // We skip reparse points 
            if ((info.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) {
                Debug.WriteLine("Skipping reparse point '{0}'", info.FullName);
                return;
            }
    
            if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
                // If our FileSystemInfo object is a directory, we call this method again on the
                // new directory.
                try {
                    DirectoryInfo dirInfo = (DirectoryInfo)info;
                    ProcessFolder(dirInfo.GetFileSystemInfos());
                }
                catch (Exception ex) {
                    // Skipping any errors
                    // Really, we should catch each type of Exception - 
                    // this will catch -any- exception that occurs, 
                    // which may not be the behavior we want.
                    Debug.WriteLine("{0}", ex.Message);
                    break;
                }
            } else {
                // If our FileSystemInfo object isn't a directory, we cast it as a FileInfo object, 
                // make sure it's not null, and add it to the list.
                var file = info as FileInfo;
                if (file != null) {
                    files.Add(file.FullName);
                }
            }
        }
    }
    

    MyMethod takes your selected path and creates a DirectoryInfo object with it, and then calls the GetFileSystemInfos() method and passes that to the ProcessFolder method.

    The ProcessFolder method looks at each FileSystemInfo object, skips the reparse points, and if the FileSystemInfo object is a directory, calls the ProcessFolder method again - otherwise, it casts the FileSystemInfo object as a FileInfo object, makes sure it's not null, and then adds the filename to the list.

    More reading:

    • FileSystemInfo Class
    • DirectoryInfo Class
    • FileInfo Class
    0 讨论(0)
提交回复
热议问题