1

I have written a sample application to debug an issue with enumerating files.

Enumerating a directory with a local path (eg C:\Data\MAN) enumerates considerably quicker than a shared directory with a UNC path (eg \\MACHINENAME\man). Even though these paths both point to the same directory on the local machine.

With 72000 files, this takes approx 10 seconds:

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Data\MAN");
FileInfo[] fileInfoTest = directoryInfo.GetFiles("*.*", 
                                                 SearchOption.AllDirectories);

With 72000 files, this takes approx 2 minutes: (where \\MACHINENAME\man is shared folder C:\Data\MAN)

DirectoryInfo directoryInfo = new DirectoryInfo(@"\\MACHINENAME\man");
FileInfo[] fileInfoTest = directoryInfo.GetFiles("*.*", 
                                                 SearchOption.AllDirectories);

Is this amount of overhead expected when using a UNC path?

HaemEternal
  • 2,229
  • 6
  • 31
  • 50
  • 1
    Can this be useful for you? http://stackoverflow.com/questions/12903054/unc-path-pointing-to-local-directory-much-slower-than-local-access – Tommaso Belluzzo Jan 16 '13 at 14:58
  • @Zarathos Very useful, although I'm not sure if it fully answers the question. Is there anyway around it for example? – HaemEternal Jan 16 '13 at 15:02
  • 2
    Part of the problem is that unless you take special steps, a UNC requires reauthentication at each connection, so you pay a lot of security overhead for each operation. To avoid the reauthentication cost, establish a persistent connection to the UNC before starting the operation. – Raymond Chen Jan 16 '13 at 15:38

1 Answers1

0

This is an old question and the comments seem to get half way there but there's no reason not to try and attempt to answer this. The file count here is part of the issue in that there are 72,000 of them. So what's going on?

Well, as already answered here, it essentially comes down to context switching between the different processes that are used when dealing with UNC paths over local paths, specifically the calling process and the SMB client + server processes. This boils down to: direct is faster. Don't go via UNC if it's a local resource, and, obviously, the more files involved the slower it'll be.

DiskJunky
  • 4,750
  • 3
  • 37
  • 66