0
namespace SignatureCreator
{
    class Program

    {


        static void Main(string[] args)
        {
            string delimiter = ",";

            // set up domain context
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);


            // find currently logged in user
            UserPrincipal up = UserPrincipal.Current;
            string displayName = up.DisplayName;
            string title = up.Description;
            string phoneNumber = up.VoiceTelephoneNumber;
            string[] userName = displayName.Split(new string[] { delimiter }, StringSplitOptions.None);
            string firstName = userName[1];
            string lastName = userName[0];
            firstName = firstName.Substring(1);
            firstName = firstName.ToUpper();
            lastName = lastName.ToUpper();
            Console.WriteLine(firstName +" "+ lastName);
            Console.WriteLine(title);
            Console.WriteLine(phoneNumber);

            DirectoryEntry entry = up.GetUnderlyingObject() as DirectoryEntry;
            System.DirectoryServices.PropertyCollection props = entry.Properties;

            /*
             * 
             */

            foreach (string propName in props.PropertyNames)
            {
                if (entry.Properties[propName].ToString() == "mobile")
                {
                    Console.WriteLine(propName + " = " + entry.Properties[propName].Value);
                }
                else
                {
                    Console.WriteLine(propName + " = NULL");
                }
            }


            Console.ReadKey();
            StringBuilder sb = new StringBuilder();

        }
    }
}

我正在尝试遍历活动目录信息以查找用户的手机号码。但我不知道如何循环并返回它。即使我只是评估propname == mobile,它也不会返回true。我究竟做错了什么?

4

1 回答 1

1

你可以试试,DirectoryEntry.RefreshCache它会将指定属性的值加载到属性缓存中。

您还可以DirectoryEntry使用DirectoryEntry.Properties.Contains方法检查给定属性是否包含在中。如果属性存在,您可以在不循环所有属性的情况下获取它的值。

entry.RefreshCache(new string[]{"mobile"});
if(entry.Properties.Contains("mobile"))
    Console.WriteLine("mobile= " + entry.Properties["mobile"].Value);
else
    Console.WriteLine("mobile  = NULL");
于 2013-09-11T03:22:08.180 回答