How can I get a list of all windows users of the local machine with the usage of .NET (C#) ?
-
You say `of a specific machine`, do you mean the local/actual machine or from a machine on the network? – Bobby May 17 '11 at 15:42
-
possible duplicate of [Get list of local computer usernames in Windows](http://stackoverflow.com/questions/5247798/get-list-of-local-computer-usernames-in-windows) – Bobby May 17 '11 at 15:46
2 Answers
Here is a blog post (with code) that explains how to do it:
http://csharptuning.blogspot.com/2007/09/how-to-get-list-of-windows-user-in-c.html
The author lists the following code (quoted from the above site):
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("users","group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
lstUsers.Items.Add(member.Name);
}
You need to add using System.DirectoryServices
at the top of your code. To change machines, you would change the Environment.MachineName
to be whatever machine you want to access (as long as you have permission to do so and the firewall isn't blocking you from doing so). I also modified the author's code to look at the users
group instead of the administrators
group.

- 16,412
- 5
- 39
- 75
-
Any idea how reliable it is? i.e. there are alternatives using WMI claiming it can be used without issues, but it has a failure rate > 0.5% – eglasius Aug 10 '12 at 08:46
-
In my experience, this method is reliable. As far as how reliable, I can't speak to that. It should be totally reliable. – IAmTimCorey Aug 10 '12 at 13:07
It depends on what you are really 'after'... if you are on a windows domain (using active directory) then you can query Active Directory IF active directory is being used to limit the users who are "authorized" to use the local machine.
If your requirements are not as stringent then you can inspect the folders in the system UserProfiles where each folder except Default User and All Users represent a user profile that has logged into the local machine. caution this may include system and/or service accounts...

- 5,051
- 3
- 30
- 57