Managementobjectsearcher投掷“未找到”例外 [英] Managementobjectsearcher throwing "not found" exception

查看:126
本文介绍了Managementobjectsearcher投掷“未找到”例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的系统获取处理器信息。它在Win 10中工作正常,但在Win 7中抛出Not Found异常。我想在两个OS中使用相同的代码。在Win 7中,如果找不到属性,我想处理异常。请帮忙。







I am trying to get processor info for my systems. It works fine in Win 10, but throws "Not Found" exception in Win 7. I want to use the same code in both OS. In Win 7 i want to handle the exception if the property is not found. Please help.



public string GetProcessor(string IPAdd)
       {
           try
           {
               string AddressIP;
               AddressIP = "\\\\" + IPAdd + "\\root\\CIMV2";
               ManagementObjectSearcher searcher = new
               ManagementObjectSearcher(AddressIP, "SELECT * FROM Win32_Processor");
               StringBuilder sb = new StringBuilder();
               foreach (ManagementObject wmi in searcher.Get())
               {
                   try
                   {
                       sb.Append("Device ID: " +
                       wmi.GetPropertyValue("DeviceID").ToString() + Environment.NewLine);
                       
                   }
                   catch
                   {
                       //this.textBox1.Text+=
                       return sb.ToString();
                   }
               }
               return sb.ToString();
           }
           catch (Exception ex)
           {
               return ex.Message;
           }
       }





我的尝试:



我试过检查wmi.GetPropertyValue()是否为null然后输出为



What I have tried:

I have tried with checking if the "wmi.GetPropertyValue()" is null then give output as ""

推荐答案

你为什么得到DeviceId?它只是一个标识符,用于唯一标识多处理器系统中的处理器。它通常只是一个字符串CPU0。就是这样。



使用WMI的问题在于硬件制造商不需要支持它。此字段以及许多其他字段可能为空或空。
Why are you getting the DeviceId? It's just an identifier that is used to uniquely identify a processor in a multiple processor system. It's usually just a string "CPU0". That's it.

The problem with using WMI is that there is no requirement for the hardware manufacturers to support it. This field, along with many others, may be blank or null.


问题不在于成员12172807获取DeviceID的原因,这就是为什么WMI在尝试之前捕获并处理异常的原因/ catch有机会处理异常。应用程序查询并在向调用程序返回可处理异常之前将异常本地(使用用户侵入式gui)抛出到WMI。
The question isn't why is Member 12172807 getting the DeviceID, it's why is WMI capturing and processing the exception before the try/catch has a chance to handle the exception. App queries and throws exception local (with user intrusive gui) to WMI before returning a "handleable" exception to calling program.


这篇关于Managementobjectsearcher投掷“未找到”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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