CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    25

    File LastWriteTime/Modified time changes

    This is probably not, technically, a C# question but it might have a C# solution so here goes . . .

    When I travel I copy files to work on from my desktop to a thumbdrive and use whatever computer I have on the road. When I return I copy the changed files back. Now I'm writing a little C# utility to identify the files that have changed, based on their LastWriteTime compared to the same file on my desktop, and automatically copy only those back to my desktop.

    I've run into a weird (Windows or XP?) problem. When I initially copy the files to the thumbdrive the modified time changes _JUST SLIGHTLY_. For example, I just copied a .doc file to my thumbdrive and the "modified time" changed from "December 11, 2008, 11:18:21 AM" to "December 11, 2008, 11:18:22 AM". WHY? That 1-second difference is enough for my program to think it's a newer file. I'm sure I can program around it (59 times out of 60) by lopping off the seconds, but why does it do this?

    (moderators: feel free to suggest an alternative forum).


    Thanks in advance!

  2. #2
    Join Date
    Dec 2008
    Posts
    25

    Re: File LastWriteTime/Modified time changes

    I've discovered the cause of the problem - my PC is NTFS; my thumbdrive is FAT32 - apparently there is a known conversion bug causing time and date information to sometimes have errors converting from one to the other with perfect precision.

    OK, so I need a C# solution. Probably, for my purposes, the simplest thing to do is to set the "seconds" part of the DateTime of the file to 0 before doing the comparisons, without changing anything else. What's the easiest way to do that? (the DateTime.Second property is read only)

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: File LastWriteTime/Modified time changes

    You can make a new instance of datetime and settings it's second value to 0.
    Code:
    DateTime now = DateTime.Now;
    DateTime dt = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0);
    In my example, replace 'now' by file.LastWriteTime, next you can do your calculations using 'dt'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured