无法从FILETIME(Windows时间)转换为dateTime(我收到不同的日期) [英] not being able to convert from FILETIME (windows time) to dateTime ( I get a different date )
问题描述
//最好的工作的时间
private static DateTime convertToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME time)
{
long highBits = time.dwHighDateTime;
highBits = highBits<<< 32;
返回DateTime.FromFileTimeUtc(highBits + time.dwLowDateTime);
}
这里我在visual studio中有一个例子来显示这种方法有时不会例如,我将显示我的电脑中的实际文件和调试。所以碰巧在我调试中的文件是:
A:\Users\Tono\Documents\Visual Studio 2010 \Projects\WpfApplication4 \WpfApplication4\obj\x86\Debug\App.g.cs
这里是我要转换为DateTime的FILETIME我需要LastWriteTime
这里你可以看到dwHighDateTime = 30136437,还有那个dwLowDateTime = -2138979250从那个文件。
当我运行我的方法加其他技术我得到以下日期:
所以到目前为止,一切似乎都很好。但是为什么当我浏览并在Windows中查找特定的文件时,我得到一个不同的日期!这是查看文件属性时获取的日期:
为什么日期不匹配?我究竟做错了什么?
您需要按位组合LS和MS值,而不是算术。
尝试:
ulong high = 30136437;
unchecked
{
int low = -2138979250;
uint uLow =(uint)low;
high = high<<< 32;
Date dt = DateTime.FromFileTime((long)(high |(ulong)uLow));
}
或以下任何一项也可以:
long highBits = time.dwHighDateTime;
highBits = highBits<<< 32;
返回DateTime.FromFileTimeUtc(highBits +(long)(uint)time.dwLowDateTime);
返回DateTime.FromFileTimeUtc(highBits |(long)(uint)time.dwLowDateTime);
return DateTime.FromFileTimeUtc(highBits +((long)low& 0xFFFFFFFF))
返回DateTime.FromFileTimeUtc(highBits |((long)low& 0xFFFFFFFF))
您可以通过添加而不是按位来摆脱,或者如果您确定值为正(并没有任何共同之处)。但是按位或更好地表达意图。
Most of the files I read get the right time when using the following method to convert:
// works great most of the time
private static DateTime convertToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME time)
{
long highBits = time.dwHighDateTime;
highBits = highBits << 32;
return DateTime.FromFileTimeUtc(highBits + time.dwLowDateTime);
}
Here I have an example in visual studio to show how this method sometimes does not work for example I will show the actual file in my computer and the debug. So the file that happens to be in my debug is:
"A:\Users\Tono\Documents\Visual Studio 2010\Projects\WpfApplication4\WpfApplication4\obj\x86\Debug\App.g.cs"
And here is the FILETIME that I am trying to convert to DateTime "I need the LastWriteTime by the way"
Here you can see that dwHighDateTime = 30136437 and also that dwLowDateTime = -2138979250 from that file.
And when I run my method plus other techniques I get the following dates:
So so far everything seems to be working great. But why is that that when I browse and look for that specific file in windows I get a different date !? Here is the date that I get when seeing the file's properties:
Why does the dates don't match? What am I doing wrong?
You need to combine the LS and MS values bitwise, not arithmetically.
Try:
ulong high = 30136437;
unchecked
{
int low = -2138979250;
uint uLow = (uint)low;
high = high << 32;
Date dt = DateTime.FromFileTime((long) (high | (ulong)uLow));
}
Or any of the following should work too:
long highBits = time.dwHighDateTime;
highBits = highBits << 32;
return DateTime.FromFileTimeUtc(highBits + (long) (uint) time.dwLowDateTime);
return DateTime.FromFileTimeUtc(highBits | (long) (uint) time.dwLowDateTime);
return DateTime.FromFileTimeUtc(highBits + ((long)low & 0xFFFFFFFF))
return DateTime.FromFileTimeUtc(highBits | ((long)low & 0xFFFFFFFF))
You can get away with adding rather than a bitwise-or if you are sure the values are positive (and have no bits in common). But bitwise-or expresses the intent better.
这篇关于无法从FILETIME(Windows时间)转换为dateTime(我收到不同的日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!