Getting All Users In A Computer

First we'll create a class to process WinApi calls.
 
This Class has been written by Michael Bright in 2003.Since then i've been using this code and extending it for network related needs.
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. namespace GettingAllUsers {  
  4.     public class NetworkAPI {  
  5.         // USER_INFO_1 - Strucutre to hold obtained user information  
  6.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
  7.         public struct USER_INFO_1 {  
  8.             public string usri1_name;  
  9.             public string usri1_password;  
  10.             public int usri1_password_age;  
  11.             public int usri1_priv;  
  12.             public string usri1_home_dir;  
  13.             public string comment;  
  14.             public int usri1_flags;  
  15.             public string usri1_script_path;  
  16.         }  
  17.         // USER_INFO_0 - Structure to hold Just Usernames  
  18.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
  19.         public struct USER_INFO_0 {  
  20.             public String Username;  
  21.         }  
  22.         // NetUserAdd - To Add Users to a local machine or Network  
  23.         [DllImport("Netapi32.dll")]  
  24.         public extern static int NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, ref USER_INFO_1 buf, int parm_err);  
  25.         // NetUserDel - To delete Users from a local machine or Network  
  26.         [DllImport("Netapi32.dll")]  
  27.         public extern static int NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username);  
  28.         // NetUserGetInfo - Returns to a struct Information about the specified user  
  29.         [DllImport("Netapi32.dll")]  
  30.         public extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, out IntPtr bufptr);  
  31.         // NetUserSetInfo - Allows us to modify User information  
  32.         [DllImport("Netapi32.dll")]  
  33.         public extern static int NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, ref USER_INFO_1 buf, int error);  
  34.         // NetUserChangePassword - Allows us to change a users password providing we have it  
  35.         [DllImport("Netapi32.dll")]  
  36.         public extern static int NetUserChangePassword([MarshalAs(UnmanagedType.LPWStr)] string domainname, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string oldpassword, [MarshalAs(UnmanagedType.LPWStr)] string newpassword);  
  37.         // NetUserEnum - Obtains a list of all users on local machine or network  
  38.         [DllImport("Netapi32.dll")]  
  39.         public extern static int NetUserEnum(string servername, int level, int filter, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resume_handle);  
  40.         // NetAPIBufferFree - Used to clear the Network buffer after NetUserEnum  
  41.         [DllImport("Netapi32.dll")]  
  42.         public extern static int NetApiBufferFree(IntPtr Buffer);  
  43.         public NetworkAPI() {  
  44.             //  
  45.             // TODO: Add constructor logic here  
  46.             //  
  47.         }  
  48.     }  
  49. }  
Add a ListBox in your form and then,Later inside your main application create a function:
  1. public void EnumerateUsers() {  
  2.     int EntriesRead;  
  3.     int TotalEntries;  
  4.     int Resume;  
  5.     listBox1.Items.Clear();  
  6.     IntPtr bufPtr;  
  7.     NetworkAPI.NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);  
  8.     if (EntriesRead > 0) {  
  9.         NetworkAPI.USER_INFO_0[] Users = new NetworkAPI.USER_INFO_0[EntriesRead];  
  10.         IntPtr iter = bufPtr;  
  11.         for (int i = 0; i < EntriesRead; i++) {  
  12.             Users[i] = (NetworkAPI.USER_INFO_0) Marshal.PtrToStructure(iter, typeof(NetworkAPI.USER_INFO_0));  
  13.             iter = (IntPtr)((int) iter + Marshal.SizeOf(typeof(NetworkAPI.USER_INFO_0)));  
  14.             listBox1.Items.Add(Users[i].Username);  
  15.         }  
  16.         NetworkAPI.NetApiBufferFree(bufPtr);  
  17.     }  
  18.     listBox1.SelectedIndex = 0;  
  19. }  
  20. Now just call EnumerateUsers inside Form_Load to populate them in your listbox at startup  
  21. private void Form1_Load(object sender, EventArgs e) {  
  22.     EnumerateUsers();  
  23. }  
And run it!
 
Getting All Users In A Computer 
 
Hope that helps!


Similar Articles