Accessing shared drive using Directory.GetFiles() (C#) method is very slow.

Atal Sarker 0 Reputation points
2025-11-17T03:45:53.85+00:00

We have application using Directory.GetFiles() to browse and locate files in Network shared drive. Since we have migrated the code from .NET framework 4.8 to .NET 8. accessing the same file/path takes 100 times longer than previously. We have been searching for the resolution and suggested recommendation from the internet did not help much.

Are there any further suggestions available?

Windows development | Windows App SDK
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 5,765 Reputation points Microsoft External Staff Moderator
    2025-11-18T09:00:50.16+00:00

    Hello @Atal Sarker ,

    They changed the way file enumeration works. Instead of letting Windows do the filtering for you, .NET now retrieves all file names and applies the search pattern in managed code.

    A few things you can try:

    Use Directory.EnumerateFiles() instead of GetFiles()

    EnumerateFiles streams results instead of loading them all at once, so it starts returning files sooner and avoids some overhead.

    Enumerate once and filter in your code

    If you call GetFiles() many times with different patterns, that is costly now because .NET applies the pattern in managed code. A better approach is to enumerate all files once and apply your own filtering logic.

    Example:

    foreach (var path in Directory.EnumerateFiles(sharePath, "*"))
    {
        if (MatchesMyCriteria(path)) { /* process */ }
    }
    

    Tune EnumerationOptions

    You can increase BufferSize (try 16 KB or 64 KB) to reduce round trips over SMB. Also check AttributesToSkip and IgnoreInaccessible if needed:

    var options = new EnumerationOptions
    {
        BufferSize = 16 * 1024,
        IgnoreInaccessible = true
    };
    
    foreach (var path in Directory.EnumerateFiles(sharePath, "*", options))
    {
        // process
    }
    

    Avoid creating FileInfo unless necessary

    Stick to strings for paths unless you really need metadata.

    Check environment factors

    • SMB signing is now required by default on Windows 11/Server 2025, which can slow down directory listings on older NAS devices. If you’re in a trusted network, testing with signing disabled can confirm if that’s the bottleneck.
    • Antivirus scanning on the share can also add latency. Try excluding the path temporarily to see if it helps.
    • Make sure the share uses SMB2 or SMB3, not SMB1.

    I hope this helps.

    1 person found this answer helpful.

  2. Q&A Assist
    2025-11-17T03:46:06.2066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    When accessing files on a network shared drive using Directory.GetFiles() in .NET 8, performance issues can arise due to various factors, especially after migrating from .NET Framework 4.8. Here are some suggestions to improve the performance:

    1. Use Directory.EnumerateFiles(): Instead of Directory.GetFiles(), consider using Directory.EnumerateFiles(). This method starts returning results immediately and can be more efficient for large directories since it does not require loading all file names into memory at once.
    2. SMB Compression: If you're using SMB for file transfers, enabling SMB compression can significantly reduce transfer times, especially for files with significant whitespace. This feature is available starting with Windows 11 and Windows Server 2022.
    3. Robocopy for Large Transfers: If you are performing large file operations, consider using robocopy for copying files, as it is optimized for performance compared to standard file operations.
    4. Network Configuration: Ensure that your network configuration is optimized. Check for any network bottlenecks or issues that could be affecting the speed of file access.
    5. Check for Antivirus/Firewall Interference: Sometimes, security software can slow down file access. Ensure that any antivirus or firewall settings are not interfering with file operations on the network drive.
    6. Profile Your Application: Use profiling tools to identify any bottlenecks in your application when accessing files. This can help you pinpoint specific areas that may need optimization.
    7. Consider Asynchronous Operations: If applicable, consider using asynchronous file access methods to improve responsiveness and potentially reduce perceived wait times.

    Implementing these strategies may help mitigate the performance issues you are experiencing with Directory.GetFiles() on a network shared drive.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.