gpt4 book ai didi

c# - 访问 Win32_OperatingSystem 的属性

转载 作者:行者123 更新时间:2023-12-03 20:37:18 25 4
gpt4 key购买 nike

我正在尝试使用存储在 Win32_OperatingSystem 属性中的值在 Windows 窗体中填充一些文本框。我使用的是 Windows 7。

下面是我使用的代码

 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() });
}

以下是我得到的异常 -

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

你能告诉我异常是什么以及我如何克服它吗?

提前致谢。

最佳答案

你可以试试下面的代码:

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);
}
}
}
}

关于c# - 访问 Win32_OperatingSystem 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15406957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com