读书人

请问FILETIME转成DataTime

发布时间: 2012-09-09 09:27:54 作者: rapoo

请教FILETIME转成DataTime
在将FILETIME转成DataTime类型时发现有8小时偏差,请大家帮忙看一下,先谢了!

private string FILETIMEtoDataTime(FILETIME time)
{
try
{

IntPtr filetime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(FILETIME)) );
IntPtr systime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(SYSTEMTIME)) );
Marshal.StructureToPtr(time,filetime,true);
FileTimeToSystemTime( filetime ,systime);
SYSTEMTIME st = (SYSTEMTIME) Marshal.PtrToStructure(systime,typeof(SYSTEMTIME));
SYSTEMTIME st1 = (SYSTEMTIME) Marshal.PtrToStructure(filetime,typeof(SYSTEMTIME));
int year=int.Parse(st.wYear.ToString());
int Month=int.Parse(st.wMonth.ToString());
int day =int.Parse(st.wDay.ToString());
int Hour =int.Parse(st.wHour.ToString());
int Minut =int.Parse(st.wMinute.ToString());
int Second=int.Parse(st.wSecond.ToString());

DateTime t=new DateTime(year,Month,day,Hour,Minut,Second);
t=t.AddHours(8);
return t.ToString("yyyy-MM-dd HH:mm:ss");
}
catch(Exception ex)
{
string ss=ex.ToString();
return "";
}

}


[解决办法]
i Dont no
[解决办法]
错了.....

直接转换



long _Value = (long)FILETIME.dwHighDateTime << 32 | (long)FILETIME.dwLowDateTime;

System.DateTime.FromFileTimeUtc(_Value );
[解决办法]
long longTime=(long)(time.dwHighDateTime<<32)+time.dwLowDateTime;
[解决办法]


t = t.Add(DateTime.Now - DateTime.UtcNow);

直接加上UTC时间把


[解决办法]
你的filetime是哪个
是System.Runtime.InteropServices.ComTypes.FILETIME?

还是win32下的FILETIME?
[解决办法]
先用FileTimeToLocalFileTime,再用FileTimeToSystemTime

读书人网 >C#

热点推荐