LP on .NET

April 5, 2010

File Timestamp Problem under Windows Embedded

Filed under: .NET,C#,Windows — Larry Parker @ 9:35 pm

I ran into quite the interesting problem today.  The QA team was testing a module of mine that syncs up files via a WCF service.  The lab had a server and two workstations.  Things were working great on one of the workstations, but the other would not sync up some files.  Every time the workstation sent its file snapshot to the server it identified files that were out of sync and dispatched a sync package that then got applied at the workstation.  This cycle kept up throughout the morning.

After much debugging and viewing of various WCF trace files (using Microsoft’s invaluable  Service Trace Viewer tool), it turned out that some of the files getting synched up at the workstation were being touched (using .NET’s File class and the SetLastWriteTimeUtc method) with a timestamp that was a second ahead of what should have been written.

For example, if I set the file’s last write time to 8:50:37 AM, the file would then show 8:50:38 AM when I looked at its properties in Windows Explorer (the Modified field).  But some other files did get the exact timestamp I wrote out.  And, everything worked fine on the other workstation.

What gives??

It dawned on me that the difference between the two workstations was the operating system.  The machine running Windows XP worked fine.  The machine having the problems was running Windows Embedded POSReady 2009.  Hmmm…

I then wrote a simple program to reproduce and isolate the problem.  Here’s the code:

static void Main(string[] args)
    DateTime ts = DateTime.Parse("2010-04-05T12:50:00");
    for (int i = 0; i <= 59; i++)
        Console.Write("Testing {0}: ", ts.ToLongTimeString());
        ts = ts.AddSeconds(1);

    Console.WriteLine("\nPress Enter to exit...");

private static void TestTimestamp(DateTime ts)
    String file = "TimestampTest.txt";
    using (File.Create(file)) ;
    File.SetLastWriteTimeUtc(file, ts);
    DateTime ts2 = File.GetLastWriteTimeUtc(file);

    if (ts.Second != ts2.Second)
        Console.Write("Error!  Expected seconds={0}, actual seconds={1}",
            ts.Second, ts2.Second);


Here’s how it ran on the Windows XP machine:


And here’s how it ran on the Windows Embedded machine:


Strange — timestamps with even seconds work, but those with odd seconds don’t!  They get “rounded” up to the next even second.

My workaround was easy enough.  Instead of comparing file timestamps down to the second, I added some tolerance for a couple of seconds.  Works for me (and for QA as well).

But I’m curious as to what’s going on under the covers with the SetLastWriteTimeUtc on Windows Embedded (all machines were running .NET 3.5 SP1).

I’ll have to post this on the Microsoft forum and will update this blog post if I find anything out.  If any readers have encountered this problem, please let me know!


1 Comment »

  1. The problem was the file system and not the operating system. The POSReady machine was running FAT and the XP machine was running NTFS.

    Here’s more info:



    Comment by Larry Parker — April 26, 2010 @ 9:35 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: