I was binding the results to a grid and you have to set the datasource to null first - this was my issue.
Not all Groups are returned via WMI
Question
Using the code below not all Group names are returned if I compare to computer management. Am I doing something wrong?
UPDATE: It does update if i restart my application
C# Code
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_GroupUser");
foreach (ManagementObject queryObj in searcher.Get())
{
String sGroupName = queryObj["GroupComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
String sUsername = queryObj["PartComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
}
}
catch (ManagementException exception)
{
MessageBox.Show("An error occurred while querying for WMI data: " + exception.Message);
}
Solution 2
OTHER TIPS
I believe Win32_GroupUser is a relationship between users and groups.
If a group has no users, it will not be included.
Try querying for Win32_Group.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow