如何更改 System.DirectoryEntry “uSNChanged"属性值到 Int64 [英] How to change System.DirectoryEntry "uSNChanged" attribute value to an Int64

查看:13
本文介绍了如何更改 System.DirectoryEntry “uSNChanged"属性值到 Int64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取目录服务对象的uSNChanged"值的 Int64 值.不幸的是,它总是作为某种 COM 对象回来.我尝试使用转换为 Int64、调用 Int64.Parse() 和调用 Convert.ToInt64().这些都不起作用.

对于给定的 DirectoryEntry 对象,此代码将显示属性:

 private static void DisplaySelectedProperties(DirectoryEntry objADObject){尝试{字符串[] 属性 = 新字符串[] {显示名称","创建时","何时更改","uSNCreated","uSNChanged",};Console.WriteLine(String.Format("显示{0}的选定属性", objADObject.Path));foreach(属性中的字符串 strAttrName){foreach (var objAttrValue in objADObject.Properties[strAttrName]){字符串 strAttrValue = objAttrValue.ToString();Console.WriteLine(String.Format(" {0, -22} : {1}", strAttrName, strAttrValue));}}Console.WriteLine();}捕获(异常前){throw new ApplicationException(string.Format("Fatal error accessing: {0} - {1}", objADObject.Path, ex.Message), ex);}}

这是输出:

<前>显示 LDAP://server/o=org/cn=obj 的选定属性displayName : 显示名称创建时间:2009 年 7 月 8 日下午 7:29:02更改时间 : 7/8/2009 10:42:23 PMuSNCreated : System.__ComObjectuSNChanged : System.__ComObject

如何将 System.__ComObject 转换为 Int64?


使用的解决方案:

这是我根据下面marc_s的解决方案使用的解决方案:

 public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger){var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);返回 highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;}

解决方案

我在我的 ADSI 浏览器中使用了这段代码BeaverTail 用 C# 编写:

Int64 iLargeInt = 0;IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

据我所知,这应该可以正常工作.

马克

I'm trying to get the Int64 value of a Directory Services object's "uSNChanged" value. Unfortunately, it is always coming back as a COM object of some kind. I've tried using casting to Int64, calling Int64.Parse(), and calling Convert.ToInt64(). None of these work.

For a given DirectoryEntry object, this code will display the properties:

    private static void DisplaySelectedProperties(DirectoryEntry objADObject)
    {
        try
        {
            string[] properties = new string[] {
                "displayName",
                "whenCreated",
                "whenChanged",
                "uSNCreated",
                "uSNChanged",
            };

            Console.WriteLine(String.Format("Displaying selected properties of {0}", objADObject.Path));
            foreach (string strAttrName in properties)
            {
                foreach (var objAttrValue in objADObject.Properties[strAttrName])
                {
                    string strAttrValue = objAttrValue.ToString();
                    Console.WriteLine(String.Format("   {0, -22} : {1}", strAttrName, strAttrValue));
                }
            }
            Console.WriteLine();
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format("Fatal error accessing: {0} - {1}", objADObject.Path, ex.Message), ex);
        }
    }

This is the output:

Displaying selected properties of LDAP://server/o=org/cn=obj
   displayName            : Display Name
   whenCreated            : 7/8/2009 7:29:02 PM
   whenChanged            : 7/8/2009 10:42:23 PM
   uSNCreated             : System.__ComObject
   uSNChanged             : System.__ComObject

How do I convert that System.__ComObject into a Int64?


Solution Used:

This is the solution I used based on marc_s's solution below:

    public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
    {
         var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         var lowPart  = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart",  System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
    }

解决方案

I'm using this snippet of code in my ADSI Browser BeaverTail which is written in C#:

Int64 iLargeInt = 0;

IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

As far as I can tell, this should work just fine.

Marc

这篇关于如何更改 System.DirectoryEntry “uSNChanged"属性值到 Int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆