Problem: .NET 2.0 File.GetLastWriteTime(path) Keeps adding 1 hour
-
When I use File.GetLastWriteTime(path) to get the date and time value of a file with a last write date of 5/8/2006 8:00a.m. the .NET 2.0 call always adds 1 to the hour so it ends up reading as 5/8/2006 9:00 a.m. How can I stop this? I need the file date to be read back exactly like the Windows explorer displays it.
-
When I use File.GetLastWriteTime(path) to get the date and time value of a file with a last write date of 5/8/2006 8:00a.m. the .NET 2.0 call always adds 1 to the hour so it ends up reading as 5/8/2006 9:00 a.m. How can I stop this? I need the file date to be read back exactly like the Windows explorer displays it.
Hi, If the file is not on the same machine as the app, then you could check to see if the system time of both machines is set the same (especially daylight saving time). Otherwise, if you are absolutely certain that it always adds exactly one hour you can use this:
CDate(File.GetLastWriteTime(path)).AddHours(-1)
Hope it helps, Johan -
Hi, If the file is not on the same machine as the app, then you could check to see if the system time of both machines is set the same (especially daylight saving time). Otherwise, if you are absolutely certain that it always adds exactly one hour you can use this:
CDate(File.GetLastWriteTime(path)).AddHours(-1)
Hope it helps, JohanNo, the problem occurs because Win32 and .NET make different choices with regard to dealing with Daylight Saving Time. For a discussion, see here: http://blogs.msdn.com/oldnewthing/archive/2003/10/24/55413.aspx To compensate, you have to adjust the time you use with SetLastWriteTime and GetLastWriteTime. When setting, adjust from .Net to Win32. When getting, adjust from Win32 to .NET. Here's some example code:
// If I have a time in the .NET environment, and I want to use it for
// SetWastWriteTime() etc, then I need to adjust it for Win32.
static DateTime AdjustTime_DotNetToWin32(DateTime time)
{
DateTime adjusted = time;
if (DateTime.Now.IsDaylightSavingTime() && !time.IsDaylightSavingTime())
adjusted = time - new System.TimeSpan(1, 0, 0);
else if (!DateTime.Now.IsDaylightSavingTime() && time.IsDaylightSavingTime())
adjusted = time + new System.TimeSpan(1, 0, 0);return adjusted;
}// If I read a time from a file with GetLastWriteTime() (etc), I need
// to adjust it for display in the .NET environment.
static DateTime AdjustTime_Win32ToDotNet(DateTime time)
{
DateTime adjusted = time;
if (DateTime.Now.IsDaylightSavingTime() && !time.IsDaylightSavingTime())
adjusted = time + new System.TimeSpan(1, 0, 0);else if (!DateTime.Now.IsDaylightSavingTime() && time.IsDaylightSavingTime()) adjusted = time - new System.TimeSpan(1, 0, 0); return adjusted;
}
void MySetLastWriteTime(String targetFile, DateTime t)
{
DateTime adjusted = AdjustTime_DotNetToWin32(t);
System.IO.File.SetLastWriteTime(targetFile, adjusted);
}DateTime MyGetLastWriteTime(String targetFile)
{
DateTime t = System.IO.File.GetLastWriteTime(targetFile);
return AdjustTime_Win32ToDotNet(t);
}