472,139 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

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 4372
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.