Kingamoon
Kingamoon

Reputation: 1487

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

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

Upvotes: 1

Views: 7253

Answers (3)

user16860989
user16860989

Reputation: 1

Windows 11 true works today:

Private Function GetLocalUsers() As List(Of String)
    Dim OpenCMD
    OpenCMD = CreateObject("wscript.shell")
    OpenCMD.run("cmd /C wmic useraccount list full | clip", 0)
    System.Threading.Thread.Sleep(1000)

    Dim result As String = Clipboard.GetText()
    Dim lines() As String = result.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
    For Each linea As String In lines
        If Mid(linea, 1, 5) = "Name=" Then
            ComboBox1.Items.Add(Mid(linea, 6, linea.Length))
        End If
    Next

    Return Nothing
End Function

Upvotes: -1

dsrdakota
dsrdakota

Reputation: 2553

Here's an improved version of Nathan W's answer:

Function GetUsers() As List(Of String)
    Dim ret As New List(Of String)
    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)
            ret.Add(username)
        End Using
    Next
    If Not ret.Contains("Guest") Then ret.Add("Guest")
    ret.Sort()

    Return ret
End Function

This function returns a list of all users on the current domain/machine from the registry. For his answer, it doesn't recognize the Guest account on my system. Don't know why.

Upvotes: 0

Nathan W
Nathan W

Reputation: 55472

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

Upvotes: 1

Related Questions