本文概述
在像Speccy这样的许多应用程序中, 你可以查看有关CPU中不同组件的详细信息。如果你正在尝试使用C#, 并且想要检索有关主板的信息, 则有一种非常简单的方法可以使用Windows的WMI类来完成。
在本文中, 我们将与你分享一种非常简单的方法来检索有关主板的详细信息。
1.添加对System.Management类的引用
为了获得有关WinForms中带有C#的主板的信息, 你将需要访问System Management类:
using System.Management;
但是, 在某些Visual Studio版本中(特别是在2010年和更高版本中), 你需要在项目中手动添加引用(.DLL)。为此, 请按照下列步骤操作:
-
右键单击项目, 添加引用
-
选择”程序集(框架)”选项卡, 然后搜索System.Management, 最后添加引用, 然后单击”确定”。
我们需要添加System.Management来在WMI类中创建查询。在此处阅读有关在msdn中检索.NET中的WMI类的更多信息。
另一方面, 请记住, 所有使用ManagementObjectSearcher类来获取系统信息的实现, 其属性值为整数值(0-100), 并且这些值与属性名称无关(例如, 使用Video_Controller GPU类)返回0到9之间的值的Architecture属性), 并且你期望一个非常特定的值(例如x86或x64), 则可能是你传递了一些信息!请阅读Microsoft开发人员网络网站上的类的文档(分别在文章的每个部分中提供), 以获取每个属性的详细说明。
例如, 如前所述, Architecture属性返回一个整数(对于你来说, 这对值没有意义), 但是此值指定下表的索引(而不是值本身):
编号 | 值 |
---|---|
0 | x86 |
1 | MIPS |
2 | Α |
3 | PowerPC |
5 | 臂 |
6 | ia64(基于Itanium的系统) |
9 | x64 |
因此由你决定如何根据msdn网站上的信息在应用程序中呈现信息。
2.创建静态MotherboardInfo类
下一步, 既然你已经可以访问WMI的管理类, 则可以注册新类(创建新类MotherboardInfo.cs), 我们将使用该类来获取主板信息。此类是静态的, 因此你无需实例化即可获得一些属性:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
static public class MotherboardInfo
{
private static ManagementObjectSearcher baseboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
private static ManagementObjectSearcher motherboardSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_MotherboardDevice");
static public string Availability
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return GetAvailability(int.Parse(queryObj["Availability"].ToString()));
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public bool HostingBoard
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
if (queryObj["HostingBoard"].ToString() == "True")
return true;
else
return false;
}
return false;
}
catch (Exception e)
{
return false;
}
}
}
static public string InstallDate
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return ConvertToDateTime(queryObj["InstallDate"].ToString());
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string Manufacturer
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["Manufacturer"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string Model
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["Model"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string PartNumber
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["PartNumber"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string PNPDeviceID
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return queryObj["PNPDeviceID"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string PrimaryBusType
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return queryObj["PrimaryBusType"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string Product
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["Product"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public bool Removable
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
if (queryObj["Removable"].ToString() == "True")
return true;
else
return false;
}
return false;
}
catch (Exception e)
{
return false;
}
}
}
static public bool Replaceable
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
if (queryObj["Replaceable"].ToString() == "True")
return true;
else
return false;
}
return false;
}
catch (Exception e)
{
return false;
}
}
}
static public string RevisionNumber
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return queryObj["RevisionNumber"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string SecondaryBusType
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return queryObj["SecondaryBusType"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string SerialNumber
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["SerialNumber"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string Status
{
get
{
try
{
foreach (ManagementObject querObj in baseboardSearcher.Get())
{
return querObj["Status"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string SystemName
{
get
{
try
{
foreach (ManagementObject queryObj in motherboardSearcher.Get())
{
return queryObj["SystemName"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
static public string Version
{
get
{
try
{
foreach (ManagementObject queryObj in baseboardSearcher.Get())
{
return queryObj["Version"].ToString();
}
return "";
}
catch (Exception e)
{
return "";
}
}
}
private static string GetAvailability(int availability)
{
switch (availability)
{
case 1: return "Other";
case 2: return "Unknown";
case 3: return "Running or Full Power";
case 4: return "Warning";
case 5: return "In Test";
case 6: return "Not Applicable";
case 7: return "Power Off";
case 8: return "Off Line";
case 9: return "Off Duty";
case 10: return "Degraded";
case 11: return "Not Installed";
case 12: return "Install Error";
case 13: return "Power Save - Unknown";
case 14: return "Power Save - Low Power Mode";
case 15: return "Power Save - Standby";
case 16: return "Power Cycle";
case 17: return "Power Save - Warning";
default: return "Unknown";
}
}
private static string ConvertToDateTime(string unconvertedTime)
{
string convertedTime = "";
int year = int.Parse(unconvertedTime.Substring(0, 4));
int month = int.Parse(unconvertedTime.Substring(4, 2));
int date = int.Parse(unconvertedTime.Substring(6, 2));
int hours = int.Parse(unconvertedTime.Substring(8, 2));
int minutes = int.Parse(unconvertedTime.Substring(10, 2));
int seconds = int.Parse(unconvertedTime.Substring(12, 2));
string meridian = "AM";
if (hours > 12)
{
hours -= 12;
meridian = "PM";
}
convertedTime = date.ToString() + "/" + month.ToString() + "/" + year.ToString() + " " +
hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString() + " " + meridian;
return convertedTime;
}
}
3.使用该类获取信息
注册该类后, 你将能够静态地转换MotherboardInfo类并获得所需的属性, 从而获得信息, 例如, 最常用的功能之一就是了解主板的型号以便搜索驱动程序:
Console.WriteLine("Motherboard Properties:");
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("Availability: " + MotherboardInfo.Availability);
Console.WriteLine("HostingBoard: " + MotherboardInfo.HostingBoard);
Console.WriteLine("InstallDate: " + MotherboardInfo.InstallDate);
Console.WriteLine("Manufacturer: " + MotherboardInfo.Manufacturer);
Console.WriteLine("Model: " + MotherboardInfo.Model);
Console.WriteLine("PartNumber: " + MotherboardInfo.PartNumber);
Console.WriteLine("PNPDeviceID: " + MotherboardInfo.PNPDeviceID);
Console.WriteLine("PrimaryBusType: " + MotherboardInfo.PrimaryBusType);
Console.WriteLine("Product: " + MotherboardInfo.Product);
Console.WriteLine("Removable: " + MotherboardInfo.Removable);
Console.WriteLine("Replaceable: " + MotherboardInfo.Replaceable);
Console.WriteLine("RevisionNumber: " + MotherboardInfo.RevisionNumber);
Console.WriteLine("SecondaryBusType: " + MotherboardInfo.SecondaryBusType);
Console.WriteLine("SerialNumber: " + MotherboardInfo.SerialNumber);
Console.WriteLine("Status: " + MotherboardInfo.Status);
Console.WriteLine("SystemName: " + MotherboardInfo.SystemName);
Console.WriteLine("Version: " + MotherboardInfo.Version);
输出数据将根据你的计算机规格而有所不同, 但与以下输出类似:
Motherboard Properties:
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Availability: Running or Full Power
HostingBoard: True
InstallDate:
Manufacturer: MSI
Model:
PartNumber:
PNPDeviceID:
PrimaryBusType: PCI
Product: H110M PRO-VD PLUS (MS-7A15)
Removable: False
Replaceable: True
RevisionNumber:
SecondaryBusType: ISA
SerialNumber: GB16464640
Status: OK
SystemName: DESKTOP-N36LKRA
Version: 1.0
编码愉快!
评论前必须登录!
注册