Get all local account names using WMI query
public List<string> GetWindowsAccounts()
{
List<string> accounts = new List<string>();
//set the scope of this search to the local machine
ManagementScope scope = new ManagementScope(@”\\” + Environment.MachineName + @”\root\cimv2″);
//connect to the machine
scope.Connect();
//use a SelectQuery to tell what we’re searching in
SelectQuery searchQuery = new SelectQuery(“select * from Win32_UserAccount where Domain='” + Environment.MachineName + “‘”);
//set the search up
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);
//loop through the collection looking for the account name
foreach (ManagementObject obj in searcherObj.Get())
accounts.Add(obj[“Name”].ToString());
return accounts;
}
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.