日期:2014-05-18  浏览次数:20877 次

请教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