0

As the title suggests, I'm looking for a way to get attributes of a large number of files in a directory, but without adding the cost of an additional disk access for each file.

For example, if I get the Name attribute of FileInfo objects in a collection, then there is no additional disk access. However if I get the LastWriteTimeUtc, then an additional disk access is made.

My code:

DirectoryInfo di = new DirectoryInfo(myDir);
FileInfo[] allFiles = di.GetFiles("*.*", SearchOption.TopDirectoryOnly);
foreach (FileInfo fInfo in allFiles)
{
    name = fInfo.Name  //no additional disk access made
    lastMod = fInfo.LastWriteTimeUtc  //further disk access made!!!
}

Does anyone know of a way I can get this information in one round trip? I would have hoped that DirectoryInfo.GetFiles() does this but no luck.

Thanks in advance.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
ianbeks
  • 2,198
  • 1
  • 23
  • 26
  • Which language? Which operating system(s)? –  Dec 21 '09 at 18:06
  • Take a look at how it works via Reflector. The reason that Name doesn't perform additional disk access is because FileInfo stores the name when it's created. And technically your `GetFiles(...)` call can just be `GetFiles()` as it does the same thing. – Agent_9191 Dec 21 '09 at 18:09
  • @Neil Butterworth: Language: c# 3.5, OS(s): Windows 2008R2 @Agent_9191: I have looked at it in Reflector and see what you mean. I'm starting to think that there's no solution other than writing my own component. Also I agree with your point regarding the GetFiles() method call, but that doesn't change anything. – ianbeks Dec 22 '09 at 11:05

2 Answers2

0

If you really care about this, you should probably write this in C using FindFirstFile/GetFileTime, etc.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
0

So, this happens by design. The LastWriteTimeUtc is lazy loaded. So nothing to do other write my own component.

ianbeks
  • 2,198
  • 1
  • 23
  • 26