Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

try
{
    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{
    throw;
}

If my root folder contains a folder for which the user doesn't have permission to access, the UnauthorizedAccessException is caught and my array is empty and all the recursion failed.

How can I handle this case and insure that my code ignore locations without permission but add the files from location with permissions?

like image 585
user1710944 Avatar asked Dec 19 '12 14:12

user1710944


2 Answers

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.
}
like image 102
Aaron Hawkins Avatar answered Oct 22 '22 02:10

Aaron Hawkins


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
like image 35
Jared Harley Avatar answered Oct 22 '22 02:10

Jared Harley