Load Users from Active Directory

  • Thread starter Thread starter Sehboo
  • Start date Start date
S

Sehboo

I posted this in another group as well...but now I am trying my luck
here...

I have followign method which loads users from active directory

private List<String> GetUserLoginsFromAD()
{
List<String> results = new List<string>();

DirectorySearcher srch = new DirectorySearcher();
srch.Filter = "(&(objectClass=user)(objectCategory=person))";

//srch.SizeLimit = 100000;
SearchResultCollection sResult = srch.FindAll();

if (null != sResult)
for (int i = 0; i < sResult.Count; i++)
if (null != sResult.Properties["SAMAccountName"] &&
sResult.Properties["SAMAccountName"].Count == 1)
results.Add(sResult.Properties["SAMAccountName"]
[0] as String);

return results;
}

But it throws an exception "A device attached to the system is not
functioning. " when it hits sResult.Count.

Any help will be appreciated.

Thanks
 
Sehboo said:
I posted this in another group as well...but now I am trying my luck
here...

I have followign method which loads users from active directory

private List<String> GetUserLoginsFromAD()
{
List<String> results = new List<string>();

DirectorySearcher srch = new DirectorySearcher();
srch.Filter = "(&(objectClass=user)(objectCategory=person))";

//srch.SizeLimit = 100000;
SearchResultCollection sResult = srch.FindAll();

if (null != sResult)
for (int i = 0; i < sResult.Count; i++)
if (null != sResult.Properties["SAMAccountName"] &&
sResult.Properties["SAMAccountName"].Count == 1)
results.Add(sResult.Properties["SAMAccountName"]
[0] as String);

return results;
}

But it throws an exception "A device attached to the system is not
functioning. " when it hits sResult.Count.

Any help will be appreciated.

Thanks



Don't know what could be the reason for the exception, and I doubt it's
"Count" property which throws an exception. Anyway you are complicating
matters which makes your code less readable, herewith a simplified code
snippet that does exactly what you are looking for:

// get only the samAccountName attribute from the AD
string[] props = {"samaccountname"};
srch.PropertiesToLoad.AddRange(props);
...
SearchResultCollection sResult = srch.FindAll();
foreach(SearchResult sr in sResult )
{
results.Add(sr.Properties["samaccountname"][0].ToString());
}
return results;

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top