Ignore folders/files when Directory.GetFiles() is denied access

前端 未结 8 1836
余生分开走
余生分开走 2020-11-22 04:27

I am trying to display a list of all files found in the selected directory (and optionally any subdirectories). The problem I am having is that when the GetFiles() method co

相关标签:
8条回答
  • 2020-11-22 04:46

    This simple function works well and meets the questions requirements.

    private List<string> GetFiles(string path, string pattern)
    {
        var files = new List<string>();
        var directories = new string[] { };
    
        try
        {
            files.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly));
            directories = Directory.GetDirectories(path);
        }
        catch (UnauthorizedAccessException) { }
    
        foreach (var directory in directories)
            try
            {
                files.AddRange(GetFiles(directory, pattern));
            }
            catch (UnauthorizedAccessException) { }
    
        return files;
    }
    
    0 讨论(0)
  • 2020-11-22 04:47

    see https://stackoverflow.com/a/10728792/89584 for a solution that handles the UnauthorisedAccessException problem.

    All the solutions above will miss files and/or directories if any calls to GetFiles() or GetDirectories() are on folders with a mix of permissions.

    0 讨论(0)
  • 2020-11-22 04:50

    I prefer using c# framework functions, but the function i need will be included in .net framework 5.0, so i have to write it.

    // search file in every subdirectory ignoring access errors
        static List<string> list_files(string path)
        {
            List<string> files = new List<string>();
    
            // add the files in the current directory
            try
            {
                string[] entries = Directory.GetFiles(path);
    
                foreach (string entry in entries)
                    files.Add(System.IO.Path.Combine(path,entry));
            }
            catch 
            { 
            // an exception in directory.getfiles is not recoverable: the directory is not accessible
            }
    
            // follow the subdirectories
            try
            {
                string[] entries = Directory.GetDirectories(path);
    
                foreach (string entry in entries)
                {
                    string current_path = System.IO.Path.Combine(path, entry);
                    List<string> files_in_subdir = list_files(current_path);
    
                    foreach (string current_file in files_in_subdir)
                        files.Add(current_file);
                }
            }
            catch
            {
                // an exception in directory.getdirectories is not recoverable: the directory is not accessible
            }
    
            return files;
        }
    
    0 讨论(0)
  • 2020-11-22 04:52

    A simple way to do this is by using a List for files and a Queue for directories. It conserves memory. If you use a recursive program to do the same task, that could throw OutOfMemory exception. The output: files added in the List, are organised according to the top to bottom (breadth first) directory tree.

    public static List<string> GetAllFilesFromFolder(string root, bool searchSubfolders) {
        Queue<string> folders = new Queue<string>();
        List<string> files = new List<string>();
        folders.Enqueue(root);
        while (folders.Count != 0) {
            string currentFolder = folders.Dequeue();
            try {
                string[] filesInCurrent = System.IO.Directory.GetFiles(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
                files.AddRange(filesInCurrent);
            }
            catch {
                // Do Nothing
            }
            try {
                if (searchSubfolders) {
                    string[] foldersInCurrent = System.IO.Directory.GetDirectories(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
                    foreach (string _current in foldersInCurrent) {
                        folders.Enqueue(_current);
                    }
                }
            }
            catch {
                // Do Nothing
            }
        }
        return files;
    }
    

    Steps:

    1. Enqueue the root in the queue
    2. In a loop, Dequeue it, Add the files in that directory to the list, and Add the subfolders to the queue.
    3. Repeat untill the queue is empty.
    0 讨论(0)
  • 2020-11-22 04:55

    Since .NET Standard 2.1, you can now just do:

    var filePaths = Directory.EnumerateFiles(@"C:\my\files", "*.xml", new EnumerationOptions
    {
        IgnoreInaccessible = true,
        RecurseSubdirectories = true
    });
    

    According to the MSDN docs about IgnoreInaccessible:

    Gets or sets a value that indicates whether to skip files or directories when access is denied (for example, UnauthorizedAccessException or SecurityException). The default is true.

    Default value is actually true, but I've kept it here just to show the property.

    The same overload is available for DirectoryInfo as well.

    0 讨论(0)
  • 2020-11-22 05:03

    This should answer the question. I've ignored the issue of going through subdirectories, I'm assuming you have that figured out.

    Of course, you don't need to have a seperate method for this, but you might find it a useful place to also verify the path is valid, and deal with the other exceptions that you could encounter when calling GetFiles().

    Hope this helps.

    private string[] GetFiles(string path)
    {
        string[] files = null;
        try
        {
           files = Directory.GetFiles(path);
        }
        catch (UnauthorizedAccessException)
        {
           // might be nice to log this, or something ...
        }
    
        return files;
    }
    
    private void Processor(string path, bool recursive)
    {
        // leaving the recursive directory navigation out.
        string[] files = this.GetFiles(path);
        if (null != files)
        {
            foreach (string file in files)
            {
               this.Process(file);
            }
        }
        else
        {
           // again, might want to do something when you can't access the path?
        }
    }
    
    0 讨论(0)
提交回复
热议问题