如何将Integer8值转换为DateTime?

Mat*_*son 3 .net active-directory

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

小智 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创建一个interop库!

     // 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();
Run Code Online (Sandbox Code Playgroud)