.net – In VB.NET, how do you get a list of all users in the current Windows machine

.netvb.netwindows

In VB.NET, how do you get a list of all users in the current Windows machine?

Best Solution

You can use the registry which requires a little bit of parsing but hey it works. Here is some code:

C#

 RegistryKey userskey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList");
        foreach (string keyname in userskey.GetSubKeyNames())
        {
                using (RegistryKey key = userskey.OpenSubKey(keyname))
                {
                    string userpath = (string)key.GetValue("ProfileImagePath");
                    string username = System.IO.Path.GetFileNameWithoutExtension(userpath);
                    Console.WriteLine("{0}", username);
                }
        }

VB

Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
For Each keyname As String In userskey.GetSubKeyNames()
    Using key As RegistryKey = userskey.OpenSubKey(keyname)
        Dim userpath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
        Dim username As String = System.IO.Path.GetFileNameWithoutExtension(userpath)
        Console.WriteLine("{0}", username)
    End Using
Next