不能够从FILETIME(窗口时间)转换为DATETIME(我得到一个不同的日期) [英] not being able to convert from FILETIME (windows time) to dateTime ( I get a different date )

查看:1070
本文介绍了不能够从FILETIME(窗口时间)转换为DATETIME(我得到一个不同的日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数我读得正确的时间使用下面的方法来转换时的文件:

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);
}

下面我在Visual Studio中一个例子来说明这种方法有时怎么不工作的例子,我将展示在我的电脑和调试的实际文件。所以,出现这种情况是在我的调试文件是:

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工作室2010\Projects\WpfApplication4 \WpfApplication4\obj\x86\Debug\App.g.cs

"A:\Users\Tono\Documents\Visual Studio 2010\Projects\WpfApplication4\WpfApplication4\obj\x86\Debug\App.g.cs"

这是我想转换为DateTime的FILETIME我需要的LastWriteTime的方式

And here is the FILETIME that I am trying to convert to DateTime "I need the LastWriteTime by the way"

在这里,你可以看到,dwHighDateTime = 30136437,也即dwLowDateTime = -2138979250从该文件

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:

所以,到目前为止一切都似乎是伟大的工作。但是,这是为什么,当我在Windows浏览并寻找特定的文件我得到一个不同的日期!?下面是看到该文件的属性时,我得到的日期:

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?

推荐答案

您需要将LS结合,MS值按位,而不是算术。

You need to combine the LS and MS values bitwise, not arithmetically.

尝试:

        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;     

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(窗口时间)转换为DATETIME(我得到一个不同的日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆