How do I loop through a PropertyCollection

36,079

Solution 1

The PropertyCollection has a PropertyName collection - which is a collection of strings (see PropertyCollection.Contains and PropertyCollection.Item both of which take a string).

You can usually call GetEnumerator to allow you to enumerate over the collection, using the usual enumeration methods - in this case you'd get an IDictionary containing the string key, and then an object for each item/values.

Solution 2

See the value of PropertyValueCollection at runtime in the watch window to identify types of element, it contains & you can expand on it to further see what property each of the element has.

Adding to @JaredPar's code


PropertyCollection collection = GetTheCollection();
foreach ( PropertyValueCollection value in collection ) {
  // Do something with the value
  Console.WriteLine(value.PropertyName);
  Console.WriteLine(value.Value);
  Console.WriteLine(value.Count);
}

EDIT: PropertyCollection is made up of PropertyValueCollection

Solution 3

usr = result.GetDirectoryEntry();
foreach (string strProperty in usr.Properties.PropertyNames)
{
   Console.WriteLine("{0}:{1}" ,strProperty, usr.Properties[strProperty].Value);
}

Solution 4

foreach(var k in collection.Keys) 
{
     string name = k;
     string value = collection[k];
}
Share:
36,079

Related videos on Youtube

Michael Kniskern
Author by

Michael Kniskern

I am currently working as an IT engineer for the government of Mesa, Arizona USA

Updated on May 08, 2020

Comments

  • Michael Kniskern
    Michael Kniskern about 4 years

    Can anyone provide an example of how to loop through a System.DirectoryServices.PropertyCollection and output the property name and value?

    I am using C#.

    @JaredPar - The PropertyCollection does not have a Name/Value property. It does have a PropertyNames and Values, type System.Collection.ICollection. I do not know the basline object type that makes up the PropertyCollection object.

    @JaredPar again - I originally mislabeled the question with the wrong type. That was my bad.

    Update: Based on Zhaph - Ben Duguid input, I was able to develop the following code.

    using System.Collections;
    using System.DirectoryServices;
    
    public void DisplayValue(DirectoryEntry de)
    {
        if(de.Children != null)
        {
            foreach(DirectoryEntry child in de.Children)
            {
                PropertyCollection pc = child.Properties;
                IDictionaryEnumerator ide = pc.GetEnumerator();
                ide.Reset();
                while(ide.MoveNext())
                {
                    PropertyValueCollection pvc = ide.Entry.Value as PropertyValueCollection;
    
                    Console.WriteLine(string.Format("Name: {0}", ide.Entry.Key.ToString()));
                    Console.WriteLine(string.Format("Value: {0}", pvc.Value));                
                }
            }      
        }  
    }
    
  • Admin
    Admin over 13 years
    Your better off using Implicit conversion in a foreach loop.
  • Despertar
    Despertar about 12 years
    I got a System.InvalidCastException: Specified cast is not valid. when using this.
  • Csaba Toth
    Csaba Toth over 11 years
    This is the right way, it seems that PropertyValueCollection was the key to the right enumeration. All the other solutions suggest another indirect indexing (or either don't work).
  • Chad
    Chad over 8 years
    Perfect, except in my case I needed to use .ToString on .Value because it couldn't be implicitly cast.
  • Bloopy
    Bloopy over 4 years
    This is the right methodology but uses the wrong type. A PropertyCollection contains PropertyValueCollection objects as per shahkalpesh's answer, not DictionaryEntry objects.