Local Users on Windows XP

  • Thread starter Thread starter Arshad Parvez
  • Start date Start date
A

Arshad Parvez

Hi,

My OS is Windows XP without any network, nor Active Directory. There
are only a few local users. I am trying to list all my local users in
a list box. Here is my code:

string strMachine;

strMachine = String.Format("WinNT://{0}", Environment.MachineName);
DirectoryEntry allUsers = new DirectoryEntry(strMachine);
foreach (DirectoryEntry user in allUsers.Children)
{
lstUsers.Items.Add(user.Name);
}

Though it doesn't generate any errors, there is a problem. I get a
long list of about 100 items which includes internal users like
ASPNet, HelpAssistant and even explorer. I have tried with appending
<<<",group">>> at the end of machine name but in that case it gives
error in <<<foreach...>>> line. I tried Directory Searcher but that
too generates Unknown Runtime Error when doing searcher.FindOne() or
searcher.FindAll(). I have tried so many examples from the web/groups
- none works.

Please help me with this.

Arshad
 
My OS is Windows XP without any network, nor Active Directory. There
are only a few local users. I am trying to list all my local users in
a list box. Here is my code:

string strMachine;

strMachine = String.Format("WinNT://{0}", Environment.MachineName);
DirectoryEntry allUsers = new DirectoryEntry(strMachine);
foreach (DirectoryEntry user in allUsers.Children)
{
lstUsers.Items.Add(user.Name);
}

Though it doesn't generate any errors, there is a problem. I get a
long list of about 100 items

The SchemaClassName for Windows users is "user". In your foreach, check if
the SchemaClass name is "user" before adding it to your list box. I've
never tried to list all users on a machine before but here is how i check
is a user exists (so i guess that this is the same if you want to list all
users):

DirectoryEntry localDirectory = null;
DirectoryEntry user = null;

//Open a connection to the local Active Directory
localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName +
",computer");

//Look for the user
try
{
user = localDirectory.Children.Find(username, "user");
}
catch(Exception e)
{
// Problem
}
 
Thanks a lot!

I have mangaed to get users with this code:

strMachine = String.Format("WinNT://{0}", Environment.MachineName);
DirectoryEntry Machine = new DirectoryEntry(strMachine);
foreach(DirectoryEntry entry in Machine.Children)
{
switch (entry.SchemaClassName)
{
case "User" :
lstTable.Items.Add(entry.Name);
break;
}
}

This solves the problem only partially, as I am still getting few
additional users which are ACTUser, ASPNET, HelpAssistant,
SQLDebugger, SUPPORT_388945a0 and VUSR_ARSHAD.

How can I limit this list to only include those user which are
explicitly created by the Administrator (myself)?

How can I list users of a certain group - I can fetch group names by
searching "Group" in SchemaClassName.

Arshad
 
Back
Top