System.DirectoryServices.PropertyCollection.Item -what returns if propertyName is invalid

I am retrieving some info from Active directory and  I had a question what happens if invalid (non-existing) Property Name to System.DirectoryServices.PropertyCollection.Item  will be passed

E.g -what  would happened if  the code reads  anEntry.Properties["userAccountControl"].Value ,but DirectoryEntry doesn't have property "userAccountControl“ .

It is not documented in MSDN  so I had to investigate it myself.

In .Net 1.1 I found that anEntry.Properties["userAccountControl”] returns not null PropertyValueCollection object, but prop.Value is null.

So the safe code to retrieve properties value should be similar the following:

      int val = 0;
      object oVal =  anEntry.Properties["userAccountControl"].Value;
      if (null!= oVal) //if property doesn't exist, than value is null
      {
          val = (int) oVal;
      }
      else   Debug.Assert(false,"anEntry.Properties[\"userAccountControl\"] not found");

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
«August»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910