The following method I created seem does not work. An error always happens on foreach loop.
NotSupportedException was unhandled…The provider does not support
searching and cannot search WinNT://WIN7,computer.
I’m querying the local machine
private static void listUser(string computer)
{
using (DirectoryEntry d= new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer"))
{
DirectorySearcher ds = new DirectorySearcher(d);
ds.Filter = ("objectClass=user");
foreach (SearchResult s in ds.FindAll())
{
//display name of each user
}
}
}
You cannot use a
DirectorySearcherwith theWinNTprovider. From the documentation:Instead, use the
DirectoryEntry.Childrenproperty to access all child objects of yourComputerobject, then use theSchemaClassNameproperty to find the children that areUserobjects.With LINQ:
Without LINQ: