Minimizing disk accesses when getting attributes of files in a directory

79 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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