In VB.NET, how do you get a list of all users in the current Windows machine?
views:
563answers:
2
+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
2009-01-27 21:38:19
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
2009-01-27 23:00:49