如何将Integer8值转换为DateTime?

时间:2011-08-24 02:16:55

标签: .net active-directory

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

2 个答案:

答案 0 :(得分:7)

来自http://www.dotnet247.com/247reference/msgs/19/96138.aspx

  

AD中的“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();

答案 1 :(得分:2)