Hi all,
I'm new to active directory programming. I am trying to write a console
program that read the LDAP attributes and generate a csv file.
I've specified the required attributes in a string array. It works properly
for most attributes but get strange error for some attributes such attributes as uidNumber, gidNumber.
The error message is like the following.
System.Runtime.InteropServices.COMException was unhandled
Message="Unknown error (0x8000500c)"
Source="CSVFileExporter"
ErrorCode=-2147463156
StackTrace:
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
Does any body know the solutions? Please help me.
The following is my codeing use to retrieve LDAP attribue and create csv file.
try
{
DirectoryEntry entry = new DirectoryEntry();
entry.Path = LDAPServerPath;
entry.Username = user;
entry.Password = password;
entry.AuthenticationType = AuthenticationTypes.None;
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = filter;
TextWriter tw = new StreamWriter("D:\\myCSV.csv", true);
mySearcher.CacheResults = false;
StringBuilder str = new StringBuilder();
//Add all properties that need to be fetched
for(int i = 0; i < expectedProperties.Length; i++)
mySearcher.PropertiesToLoad.Add(expectedProperties[i]);
mySearcher.SearchScope = SearchScope.Subtree;
SearchResultCollection resultUsers = mySearcher.FindAll();
foreach (SearchResult srUser in resultUsers)
{
try
{
DirectoryEntry de = srUser.GetDirectoryEntry();
for(int c = 0; c < expectedProperties.Length; c++)
{
if (de.Properties.Contains(expectedProperties[c]))
{
str.Append(de.Properties[expectedProperties[c]].Value.ToString());
}
else
{
str.Append("");
}
if(c != expectedProperties.Length - 1)
str.Append(seperator);
}
de.Close();
tw.WriteLine(str);
str.Remove(0, str.Length);
}
catch(Exception ee)
{
throw ee;
}
}
tw.Close();
}
catch (COMException ex)
{
throw ex;
}
catch (Exception e)
{
throw e;
}
Regards,
gakuci