2009-01-27 7 views

Répondre

1

Vous pouvez utiliser le registre qui nécessite un peu d'analyse mais bon, ça marche. Voici un 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 
0

est ici une version améliorée de Nathan W's réponse:

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 

Cette fonction renvoie une liste de tous les utilisateurs du domaine/machine en cours à partir du registre. Pour sa réponse, il ne reconnaît pas le compte Invité sur mon système. Je ne sais pas pourquoi.

Questions connexes