Search code examples
c#windows-server

Directory.EnumerateDirectories() hang on some network folders


I have this simple C# code:

foreach (string s in Directory.EnumerateDirectories(root))
{
    Console.WriteLine(s);
}

root is a string (UNC) to a network share (Windows) containing 10520 files and no subfolders. I would expect Directory.EnumerateDirectories(root) to return an empty collection, but it hangs. I have waited several minutes.

The DOS command "dir" work as expected and even "Properties" in Windows Explorer completes the task in 5 seconds reporting "10 520 Files, 0 Folders"

My guess is that there is some limit either on the file server or on my computer.


Solution

  • The issue is most likely with .net itself. Sometimes (if not always) it's slower than native Windows API.

    Command Line and Explorer are working with WinAPI that is why they are so fast.

    Fortunately there is a possibility to invoke WinAPI via C# using P/Invoke.

    There are a lot of examples, like this.