I am developing a .Net 4.6.1 desktop application that is using the following code to determine the human-friendly OS name for logging purposes (as suggested in this answer).
public static string GetOSFriendlyName()
{
string result = string.Empty;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
foreach (ManagementObject os in searcher.Get())
{
result = os["Caption"].ToString();
break;
}
return result;
}
However, I'm getting error reports from some of my users indicating that ManagementObjectSearcher.Get()
is throwing an UnauthorizedAccessException
:
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Management.ThreadDispatch.Start()
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObjectSearcher.Initialize()
at System.Management.ManagementObjectSearcher.Get()
[my code]
I know next to nothing about WMI (having simply copied code out of the above SO answer), and so I have no idea what might cause that exception to be thrown, and I haven't been able to reproduce the exception myself. Googling has only turned up results for people trying to connect to WMI remotely, which I'm not (I don't think!) doing.
What might cause this exception to be thrown for some users but not others?
Make sure the users have the correct ACLs as some WMI queries require elevated rights to execute.