Accessing the properties of Win32_OperatingSystem

2019-07-15 06:47发布

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.

The following is the code I am using

 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?

Thanks in advance.

2条回答
小情绪 Triste *
2楼-- · 2019-07-15 06:52

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);
            }
        }
    }
}
查看更多
你好瞎i
3楼-- · 2019-07-15 07:03

you don't use property to get Value in a foreach loop. Keep in mind that property.Value can be null. property.Value is a object that can be a String or a Array. This is a extract of a code I made that can help you :

mo  //ManagementObject
    .Properties
    .OfType<PropertyData>()
    .ToList()
    .ForEach(p =>
    {
        String str = String.Empty;
        if (p.Value != null)
             if (p.Value.GetType().BaseType == typeof(Array)) // Value is a array, special string creation
             {
                 Array list = (p.Value as Array);
                 foreach (object o in list)
                      str += o.ToString() + "-";
                 if (list.Length > 0)
                      str = str.Substring(0, str.Length - 1);
             }
             else // value is already a string
                 str = p.Value.ToString();

         this.ListDuet
             .Add(new Duet()
             {
                 Key = Convert.ToString(p.Name),
                 Value = str
             });
     });

Duet is a class I made to easily manage my data.

查看更多
登录 后发表回答