2011-08-24 134 views
3

如何将Integer8类型的值转换为DateTime类型的值?特别是,我试图以人类可读的形式获取Active Directory用户属性的accountExpiresSearchResult.GetDirectoryEntry.Properties("accountExpires")返回值“9223372036854775807”。如何将Integer8的值转换为DateTime?

回答

7

从在AD http://www.dotnet247.com/247reference/msgs/19/96138.aspx

的 “Integer8” 是保持两个32位的属性的对象,称为 LowPart和HighPArt。这样的属性作为通用RCW (__ComObject)返回,您需要做的是打开底层对象或将其转换为LargInteger COM类型。之后,您必须将 这两个属性合并为一个长整型(64位),如果该值表示日期 ,则必须将格式从FileTime转换为DateTime。

以下显示如何检索“lastLogon”日期属性。 !设置一个 对activeds.tlb的引用,或使用 tlbimp.exe创建一个互操作库!

 // Use a cast ... 
    li = pcoll["lastLogon"].Value as LargeInteger; 
    // Or use CreateWrapperOfType 
    // li = (LargeIntegerClass)Marshal.CreateWrapperOfType(pcoll["lastLogon"].Value, 
typeof(LargeIntegerClass)); 
    // Convert to a long 
    long date = (((long)(li.HighPart) << 32) + (long) li.LowPart); 
    // convert date from FileTime format to DateTime 
    string dt = DateTime.FromFileTime(date).ToString(); 
+0

谢谢你,这是真的很有帮助! –