FILETIME 互转 DateTime 待验证

本文提供了一段实用的C#代码示例,演示了如何将System.DateTime类型转换为System.Runtime.InteropServices.FILETIME类型,并反过来进行转换。这对于处理.NET环境中Win32 API返回的FILETIME结构非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值