Why does ManagementObjectSearcher.Get() throw an UnauthorizedAccessException?

♀尐吖头ヾ 提交于 2020-01-06 04:04:32

问题


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?


回答1:


Make sure the users have the correct ACLs as some WMI queries require elevated rights to execute.



来源:https://stackoverflow.com/questions/37200906/why-does-managementobjectsearcher-get-throw-an-unauthorizedaccessexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!