FILETIME 互转 DateTime 待验证

来源:互联网 发布:淘宝店铺客服中心图片 编辑:程序博客网 时间:2024/06/16 13:22

http://blog.163.com/mingwang_ok/blog/static/18436570200773075452554/


HOW TO: convert a System.Runtime.InteropServices.FILETIME into a System.DateTime and back again

Added On: 12/14/2003

Blurb

Eventually, everyone works with an unmanaged Win32 API in .NET that returns you information in the form of a FILETIME struct. But, a .NET programmer is used to working with a DateTime struct. This code converts FILETIME to DateTime and DateTime to FILETIME... just everything a growing programmer needs.

Sample C# Code


using System;using System.Runtime.InteropServices;namespace ClassLibrary3{    class __Loader    {        // HOWTO: convert a System.Runtime.InteropServices.FILETIME         //        into a System.DateTime and back again        static void Main()        {            FILETIME ft = new FILETIME();            /////////////////////////////////////////////////////////////////            //from System.DateTime to System.Runtime.InteropServices.FILETIME            /////////////////////////////////////////////////////////////////            long hFT1 = DateTime.Now.ToFileTimeUtc();            ft.dwLowDateTime = (int) (hFT1 & 0xFFFFFFFF);            ft.dwHighDateTime = (int) (hFT1 >> 32);            /////////////////////////////////////////////////////////////////            //from System.Runtime.InteropServices.FILETIME to System.DateTime            /////////////////////////////////////////////////////////////////            long hFT2 = (((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;            DateTime dte = DateTime.FromFileTimeUtc(hFT2);            Console.WriteLine(dte);        }    }}

原创粉丝点击