I use C # to write a simple program to read Active Directory and display the value stored in the AD field in a Windows programmatic form.
If the property does not exist, the program crashes, below is my code, how can I catch this and move on to the next field without doing a try / catch for each attribute?
DirectoryEntry usr = new DirectoryEntry("LDAP://" + domain, username, password);
DirectorySearcher searcher = new DirectorySearcher(usr);
searcher.Filter = "(sAMAccountName=" + GlobalClass.strUserName + ")";
searcher.CacheResults = false;
searcher.SearchScope = SearchScope.Subtree;
searcher.PropertiesToLoad.Add("givenName");
searcher.PropertiesToLoad.Add("telephoneNumber");
textBoxFirstName.Text = usr.Properties["telephoneNumber"].Value.ToString();
source
share