为什么 ManagementObjectSearcher.Get() 会抛出 UnauthorizedAccessException? [英] Why does ManagementObjectSearcher.Get() throw an UnauthorizedAccessException?

查看:41
本文介绍了为什么 ManagementObjectSearcher.Get() 会抛出 UnauthorizedAccessException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 .Net 4.6.1 桌面应用程序,它使用以下代码来确定用于日志记录的人性化操作系统名称(如 这个答案).

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;
}

但是,我从我的一些用户那里收到错误报告,指出 ManagementObjectSearcher.Get() 正在抛出一个 UnauthorizedAccessException:

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]

我对 WMI 几乎一无所知(只是简单地从上面的 SO 答案中复制了代码),所以我不知道什么可能导致抛出该异常,而且我自己也无法重现该异常.谷歌搜索只为尝试远程连接到 WMI 的人提供了结果,而我没有(我不认为!)这样做.

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?

推荐答案

确保用户拥有正确的 ACL,因为某些 WMI 查询需要提升的执行权限.

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

这篇关于为什么 ManagementObjectSearcher.Get() 会抛出 UnauthorizedAccessException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆