Problem : you are returning from the foreach loop so that it returns only first item.
Solution 1: if you are sure that you have only one item that willbe returned from foreach loop then you can save that in a string variable and return at the end.
Try This:
private string GetWMIProperty(string property)
{
string value = string.Empty;
SelectQuery selectQuery = new SelectQuery("Win32_OperatingSystem");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery))
{
foreach (ManagementObject mo in searcher.Get())
{
value = mo[property].ToString();
}
}
return value;
}
Solution 2: if your foreach loop can iterate for more than 1 time then its better to save your items into some Collection and then return that Collection instead of string
Try This:
private List<string> GetWMIProperty(string property)
{
List<string> value =new List<string>();
SelectQuery selectQuery = new SelectQuery("Win32_OperatingSystem");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery))
{
foreach (ManagementObject mo in searcher.Get())
{
value.Add(mo[property].ToString());
}
}
return value;
}