访问Win32_OperatingSystem的属性属性、Win32_OperatingSystem

2023-09-06 16:05:04 作者:谈爱不谈情。

我想填充使用存储在Win32_OperatingSystem的属性值在Windows窗体的几个文本框。我使用的是Windows 7操作系统。

I am trying to populate a few textboxes in a windows form using the values stored in the properties of the Win32_OperatingSystem. I am using a windows 7.

以下是code我使用

 ArrayList prName = new ArrayList();
        ArrayList prValue = new ArrayList();
        int i = 0;
        ManagementClass msClassOS = new ManagementClass("Win32_OperatingSystem");
        msClassOS.Options.UseAmendedQualifiers = true;
        PropertyDataCollection properties = msClassOS.Properties;
        foreach (PropertyData property in properties)
        {
            prName.Add(property.Name);
        }

        foreach (PropertyData property in properties)
        {
            prValue.Add(new string[] { msClassOS.GetPropertyValue("Value").ToString() });
         }

以下是例外,我得到 -

The following is the exception I am getting -

System.Management.ManagementException: Not found 
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.PropertyData.RefreshPropertyInfo()
at System.Management.PropertyDataCollection.get_Item(String propertyName)
at System.Management.ManagementBaseObject.GetPropertyValue(String propertyName)
at NetworkMonitoringSoftware.Form1.tabControl1_Selected(Object sender, TabControlEventArgs e) in C:\Users\OWNER\Documents\Visual Studio 2010\Projects\NetworkMonitoringSoftware\NetworkMonitoringSoftware\Form1.cs:line 

您可以告诉我的例外是什么,以及如何,我可以克服它?

Can you tell me what the exception is and how I can overcome it?

在此先感谢。

推荐答案

您可以试试下面code:

you can try below code :

using System;
using System.Management;
namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {             
            try
            {
                ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
                foreach (ManagementObject queryObj in osClass.GetInstances())
                {
                    foreach (PropertyData prop in queryObj.Properties)
                    {
                        //add these to your arraylist or dictionary 
                      Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
                    }                    
                }
            }
            catch (ManagementException e)
            {
                //MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}