1
我正在開發一個.Net 4.6.1桌面應用程序,它使用以下代碼來確定用於記錄目的的人性化操作系統名稱(如this answer中所建議的)。爲什麼ManagementObjectSearcher.Get()會引發UnauthorizedAccessException?
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(有簡單複製代碼出來的上面的答案),所以我不知道什麼可能會導致異常被拋出,我一直無法自己重現異常。谷歌搜索只會給那些試圖遠程連接到WMI的人帶來結果,而我並沒有(我不認爲!)這樣做。
什麼可能會導致某些用戶引發此異常而不引發其他用戶?