469,267 Members | 938 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Local Users on Windows XP

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
Jul 21 '05 #1
2 4262
Elp
On 16 Feb 2005 07:13:17 -0800, Arshad Parvez wrote:
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
}
Jul 21 '05 #2
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

-------------------------------------------
Elp <ro********@REMOVEME.hotmail.com> wrote in message news:<20*****************************@40tude.net>. ..
On 16 Feb 2005 07:13:17 -0800, Arshad Parvez wrote:

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
}

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Arshad Parvez | last post: by
4 posts views Thread by rdemyan via AccessMonster.com | last post: by
3 posts views Thread by dion.naidoo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.