25 November 2008

C# WMI Howto List Windows OS User List With Login Names

It is not easy to reach windows login users list in C#.

There are several ways to get this list, like WMI (windows media instrumentation).
But here I will use a different way, let's read the registry and list the win user list.
string users_reg_key=
@"SOFTWAREMicrosoftWindowsCurrentVersionExplorerDocFolderPaths";
With using this registry key, we can have all users who login into that os. Let's write a simple function to read this registry key in C#.
 public string[] ListWinUsersList()
{
//The registry key for reading user list.

RegistryKey key =
Registry.LocalMachine.OpenSubKey(users_reg_key, true);

string[] winusers;

if (key != null && key.ValueCount > 0)
{
winusers = key.GetValueNames();
}
return winusers[];
}
And now we get a string array filled with windows users list.

1 comment:

Anonymous said...

I'm pretty sure this key only contains paths for the current user, not ALL users. SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList comes close, but not if the username has been changed post account creation.