【问题标题】:Why does ManagementObjectSearcher.Get() throw an UnauthorizedAccessException?为什么 ManagementObjectSearcher.Get() 会抛出 UnauthorizedAccessException?
【发布时间】:2016-09-09 02:43:48
【问题描述】:

我正在开发一个 .Net 4.6.1 桌面应用程序,它使用以下代码来确定人类友好的操作系统名称以用于日志记录(如 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

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 的人提供了结果,而我没有(我不认为!)这样做。

什么可能导致某些用户抛出此异常,而其他用户则不会?

【问题讨论】:

    标签: .net wmi


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2021-03-05
      • 2021-03-19
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多