The following code generates a FileNotFoundException (using .NET 2.0):
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace LazyFileInfoTest
{
class Program
{
static void Main(string[] args)
{
File.WriteAllText("Test.txt", "Hello World!");
DirectoryInfo di = new DirectoryInfo(".");
FileInfo[] files = di.GetFiles();
File.Delete("Test.txt");
foreach (FileInfo fi in files)
{
Console.WriteLine(string.Format("{0} Last Modified: {1}", fi.Name, fi.LastWriteTime));
Console.WriteLine(string.Format("{0} Last Modified: {1}", fi.Name, fi.LastAccessTime));
//Exception when we reach test.txt
Console.WriteLine(string.Format("{0} length is: {1}", fi.Name, fi.Length));
}
}
}
}
It looks like the Length property is lazy. Is there any reason why? This seems like an inconsistency because it is not the case with other properties. (See http://stackoverflow.com/questions/1448716/net-fileinfo-lastwritetime-fileinfo-lastaccesstime-are-wrong for a counter example.)
Thanks.