开发者

Fastest .NET way to retrieve the most metadata from files in a remote network share?

Of the available .NET System.IO methods/classes, what is the most efficient way to retrieve an entire directory listing on a remote network share (assume a slow, non-LAN speed link)?

For 10,000+ files, need to grab:

  1. Name
  2. Size
  3. Date Last Modified
  4. Date Created

There appears to be a huge performance difference in the amount of time it takes to loop through FileInfo objects for this information vs. the amount of time that Windows Explorer can display the sa开发者_开发百科me thing.


Yes, this is a side-effect of a design choice made in .NET 1.0 for the FileInfo class. It doesn't store the property values when the FileInfo object is constructed, it is retrieved from the file when you use the property getter. That way you always get the up-to-date value of the property. Which of course matters a great deal for the size and date properties, they mutate easily. The round-trip through the network however makes it slow.

It was solved in .NET 4 with the added DirectoryInfo.EnumerateXxxx() methods. The emphasis on an enumerator made it now obvious that you got a potentially stale copy of the file info. But avoiding the round-trip.

Solves your problem if you can use .NET 4. You'll need to pinvoke FindFirstFile, FindNextFile, FindClose if you can't.


For best performance, you're probably going to want to use Win32 APIs like FindFirstFile, FindNextFile, GetFileAttributesEx, and GetFileSizeEx.

If you'd like to avoid Win32 calls, Directory.EnumerateFiles is more efficient than Directory.GetFiles, because it lazily enumerates over the files as they're requested, which may internally use more efficient Win32 APIs. However, since you're going over the network, you may actually want to call Directory.GetFiles to grab them all at once. Experiment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜