Optimizing Dictionary.EnumerateFiles over the Web

I currently have a program that scans network resources. To do this, he first lists all the files and directories on the shared resource. This is a very slow process. I am currently using the code below, taken from the 2011 answer on this site.

static class SafeWalk
    {
        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)
            {
                Console.WriteLine(ex.Message);
                return Enumerable.Empty<string>();
            }
        }
    }

The problem is that everything else in the program is multithreaded and optimized for speed. This is the only area that seriously holds me back. It may take several minutes to list files in a network share. This is on the intranet, and there are gigabit connections or more between my machine and the server.

, . -, , ? , - , .

+4
3

PInvoke FindFirstFileEx FIND_FIRST_EX_LARGE_FETCH. , .

+2

, ? ? , . - , , . , , , ?

0

Fast Directory Enumerator .

?

Directory.GetFiles DirectoryInfo.GetFiles Directory.EnumerateFile() . , .

Internally, Directory.GetFiles Win32 FindFirstFile/FindNextFile. , , GetFiles() , . .

FastDirectoryEnumerator FileData. , .

-1

All Articles