Connecting Tech Pros Worldwide Forums | Help | Site Map

Load Users from Active Directory

Sehboo
Guest
 
Posts: n/a
#1: Dec 10 '07
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<StringGetUserLoginsFromAD()
{
List<Stringresults = 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[i].Properties["SAMAccountName"] &&
sResult[i].Properties["SAMAccountName"].Count == 1)
results.Add(sResult[i].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

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#2: Dec 10 '07

re: Load Users from Active Directory


"Sehboo" <MasoodAdnan@gmail.comwrote in message
news:bfa511b6-d5e5-4981-a8bc-a8b683f7a3b8@18g2000hsf.googlegroups.com...
Quote:
>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<StringGetUserLoginsFromAD()
{
List<Stringresults = 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[i].Properties["SAMAccountName"] &&
sResult[i].Properties["SAMAccountName"].Count == 1)
results.Add(sResult[i].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.

Closed Thread