views:

563

answers:

2

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

+2  A: 

Sample C# code here: http://www.andreas-kraus.net/blog/how-to-list-all-windows-users/

Converting the code from C# to VB is trivial.

Serge - appTranslator
A: 

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
Nathan W