[英]C# P/Invoke Attribute
New to C# Compact edition 6.5. 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.
我正在尝试在似乎与实际系统时间相差5小时的文件上设置日期时间。 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? 只是这样做,创建日期要提前5个小时...如果我尝试设置CreationTime,会收到一个编译错误,说属性为Readonly,是吗?
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? 所以我的问题是,由于我不熟悉C#,如何在CE框架中访问“只读”属性? I see mentioning of P/Invoke but seems to work on methods only and not attributes.
我看到提到了P / Invoke,但似乎仅对方法有效,而对属性无效。 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 我已经尝试过这种解决方案 ,即使我将其发送为当前本地时间,仍然可以获得写UTC的文件
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
00:00:00.0140000
Which seems to be correct to me. 这似乎对我来说是正确的。
You can't modify the CreationTime of a file.
您无法修改文件的CreationTime。
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 如果您愿意使用P / Invoke设置时间,则可以查看类似的问题-c#-在Compact Framework中更改文件LastWriteDate
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).
如果文件的创建时间有问题,我会考虑检查系统的时间设置(包括时区)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.