Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem: .NET 2.0 File.GetLastWriteTime(path) Keeps adding 1 hour

Problem: .NET 2.0 File.GetLastWriteTime(path) Keeps adding 1 hour

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zonkerman
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • Z zonkerman

      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.

      J Offline
      J Offline
      Johan Hakkesteegt
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • J Johan Hakkesteegt

        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

        C Offline
        C Offline
        Cheeso
        wrote on last edited by
        #3

        No, 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);
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups