Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IADsLargeInteger的命名空间_C#_Active Directory_Com Object - Fatal编程技术网

C# IADsLargeInteger的命名空间

C# IADsLargeInteger的命名空间,c#,active-directory,com-object,C#,Active Directory,Com Object,我正在寻找将COM对象转换为DateTime的方法,我看到了很多关于这个问题的文章 (像这个吗- 而这个—— ) 但是,所有这些文章都在讨论如何从接口IADsLargeInteger使用对象 我试图查找此接口的名称空间,但找不到任何线索。下面是一个代码示例,包括从AD类型转换为DateTime所需的所有内容: using System.DirectoryServices; using System.DirectoryServices.AccountManagement; using Active

我正在寻找将COM对象转换为
DateTime
的方法,我看到了很多关于这个问题的文章 (像这个吗- 而这个—— )

但是,所有这些文章都在讨论如何从接口
IADsLargeInteger
使用对象


我试图查找此接口的名称空间,但找不到任何线索。

下面是一个代码示例,包括从AD类型转换为DateTime所需的所有内容:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using ActiveDs; // Namespace added via ref to C:\Windows\System32\activeds.tlb

private DateTime? getLastLogin(DirectoryEntry de)
{
    Int64 lastLogonThisServer = new Int64();

    if (de.Properties.Contains("lastLogon"))
    {
        if (de.Properties["lastLogon"].Value != null)
        {
            try
            {
                IADsLargeInteger lgInt =
                (IADsLargeInteger) de.Properties["lastLogon"].Value;
                lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;

                return DateTime.FromFileTime(lastLogonThisServer);
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
    return null;
}
使用System.DirectoryServices;
使用System.DirectoryServices.AccountManagement;
使用ActiveDs;//通过引用将命名空间添加到C:\Windows\System32\activeds.tlb
私人约会时间?getLastLogin(目录条目de)
{
Int64 lastLogonThisServer=new Int64();
if(de.Properties.Contains(“lastLogon”))
{
if(de.Properties[“lastLogon”].Value!=null)
{
尝试
{
IADSlargent整数lgInt=
(IADsLargeInteger)de.Properties[“lastLogon”].Value;

lastLogonThisServer=((long)lgInt.HighPart除了前面的答案之外,它显示了从
IADsLargeInteger
变量获取值的正确代码,我只想说,如果您只需要此接口,无需添加对COM类型库的引用

要使用COM类型,您可以在自己的代码中定义接口:

[ComImport, Guid("9068270b-0939-11d1-8be1-00c04fd8d503"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface IAdsLargeInteger
{
    long HighPart
    {
        [SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
    }

    long LowPart
    {
        [SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
    }
}
并以同样的方式使用它:

var largeInt = (IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;
var datelong = (largeInt.HighPart << 32) + largeInt.LowPart;
var dateTime = DateTime.FromFileTimeUtc(datelong);
var largeInt=(IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;

var datelong=(largeInt.HighPart您不需要引用ActiveDs.dll-相反,您可以这样做

我已经在.NET标准2.0和.NET 5中验证了这一点

。。。
Int64 lastLogonThisServer=ConvertADSLargeIntegerToInt64(de.Properties[“lastLogon”].Value);
返回DateTime.FromFileTime(lastLogonThisServer);
...
公共静态Int64转换器ADSLARGEINTEGERTOINT64(对象adsLargeInteger)
{
var highPart=(Int32)adslageinteger.GetType().InvokeMember(“highPart”,System.Reflection.BindingFlags.GetProperty,null,adslageinteger,null);
var lowPart=(Int32)adslageinteger.GetType().InvokeMember(“lowPart”,System.Reflection.BindingFlags.GetProperty,null,adslageinteger,null);
返回高部分*((Int64)UInt32.MaxValue+1)+低部分;
}

这个答案值得称赞。

我找到了答案。它是ActiveDs命名空间。要找到该命名空间,需要添加对C:\Windows\System32\ActiveDs.tlb的引用。ActiveDs C:\Windows\System32\ActiveDs.tlb确实是在.NET?中使用ActivieDirectory所必需的。我只使用
System.DirectoryServices.AccountManagement
否使用ActiveDirectory通常不需要t。但是,如果您想在AD中使用许多日期类型,例如lastLogon,则需要IADsLargeInteger类型,它是ActiveDs库的一部分。我没有看到任何解决方法,但我也希望如此!您是否使用System.DirectoryServices转换了这些日期.AccountManagement?对于示例,我在github中看到了以下源代码:和其他链接。
maxPwdAge=TimeSpan.FromTicks((long)sr.Properties[“maxPwdAge”][0])
@Kiquenet,只有当从
DirectorySearcher
返回的
ResultPropertyValueCollection
读取属性时,才可以直接转换为long。来自
DirectoryEntry
的属性是
PropertyValueCollection
并且可以包含其他ADSI COM对象,如
IADsLargeInteger
是代码中的错误。要获得正确的结果,必须按以下方式进行更改:
lastLogonThisServer=((long)lgInt.HighPart