0

New to C# Compact edition 6.5. I am trying to set the datetime on a file which seems to be off by 5 hours from the actual system time. I am doing only this to create the file:

        FileStream fs= File.Create(name);  

Just doing this the Created date is 5 hours ahead...if I try and set the CreationTime I get a compile error saying the Attribute is Readonly, seriously?

        FileInfo fi = new FileInfo(name);
        fi.CreationTime = date;

So my question is since I am new to C# how do you get access to a "readonly" Attribute in the CE framework? I see mentioning of P/Invoke but seems to work on methods only and not attributes. Anyone can given a quick demo on how to do this?

I've tried this solution and still get the file writing UTC even though I send it the current local time

Community
  • 1
  • 1
JPM
  • 9,077
  • 13
  • 78
  • 137
  • 2
    Consider *preventing* the problem instead of "fixing" it. I'd begin by answering the following question: in your time zone, what's the offset from UTC? – Stephen Cleary Aug 22 '11 at 16:40
  • I'm in central the system timeszone is set to central so not sure why it is creating the file and setting it to UTC. – JPM Aug 22 '11 at 16:56

2 Answers2

1

I just ran this:

[MTAThread]
static void Main()
{
    var name = "\\foo.txt";
    var info = new FileInfo(name);
    using (info.Create()) { }
    info.Refresh();
    var createTime = info.CreationTime;
    var now = DateTime.Now;
    var delta = now - createTime;
    Debug.WriteLine(delta.ToString());
}

And got this output:

00:00:00.0140000

Which seems to be correct to me.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • The display time is correct in the Debug output but when I look at the creation time using the Windows File Explorer the date is again 5hrs ahead of my local time. – JPM Aug 22 '11 at 19:27
  • So the File Explorer is the "bug". It's displaying UTC times, not local times. – ctacke Aug 22 '11 at 19:54
  • Lol or the File Explorer is correct and the CE is translating the actual date...who knows at this point? – JPM Aug 22 '11 at 20:53
  • 1
    The fact the code above delivers expected results shows that the OS is not doing any translation, so it's got to be at the display end. Explorer must be showing you UTC times, as confusing as that may be. FWIW, on a general CE device they are displayed in local time, so it's actually a WinMo/WEH behavior. – ctacke Aug 22 '11 at 20:59
  • You can not know for sure that the function to retrieve the creation is doing UTC to local time translation unless you specifically can look at the Native code. But either way I will mark this as the answer and deal with this as there seems to be know other way to work around this. – JPM Aug 23 '11 at 14:01
0

You can't modify the CreationTime of a file. It's set once and only once when the file is created. If you're willing to use P/Invoke to set the time, you can check out this similar question - c# - Change file LastWriteDate in Compact Framework

Instead of hacking the problem, though, you should fix the root cause. If there's an issue with the creation time of the file, I would consider checking your system's time settings (including timezone).

Community
  • 1
  • 1
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Yes I've tried this method and it does not work, hence why I asked it again so can you please take off the minus vote on my question? – JPM Aug 22 '11 at 17:03