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/2003Blurb
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); } } }

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

被折叠的 条评论
为什么被折叠?



