Search code examples
c#filedatetimefileinfo

Why do File.GetLastWriteTimeUtc and FileInfo.LastWriteTime return different values for GMT?


So I have a file..

var myFile = C:\docs\MyFile.pptx

and when I noticed that

FileInfo fi = new FileInfo(myfile);
var lastModified = fi.LastWriteTime;

and

var lastModified = File.GetLastWriteTimeUtc(myFile);

are returning different values. Why is this? The FileInfo value corresponds to the one displayed in Windows Explorer Date Modified column. Why do they differ? I would have expected them to would return exactly the same value.


Solution

  • I've actually checked the code in Reflector and they both do the exact same thing, i.e:

    return DateTime.FromFileTimeUtc((long) data.ftLastWriteTimeHigh << 32 | (long) data.ftLastWriteTimeLow);
    

    vs

    return DateTime.FromFileTimeUtc((long) this._data.ftLastWriteTimeHigh << 32 | (long) this._data.ftLastWriteTimeLow);
    

    I've also tested it and dates are the same. You must have accidentally compared Utc with a non-Utc function.