C#获取系统硬件参数

获取系统硬件参数 方法 // 获取 var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory"); // 遍历 foreach (var memory in searcher.Get()) {  

获取系统硬件参数

方法

// 获取
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
// 遍历
foreach (var memory in searcher.Get())
{
    // 赋值(查看微软官方文档/WQL了解)
    var manufacturer = memory.GetPropertyValue("Manufacturer");
    var Capacity = memory.GetPropertyValue("Capacity");
    // 输出
    richTextBox1.AppendText($"内存品牌:\t{tag}\n");
    richTextBox1.AppendText($"内存大小:\t{Convert.ToInt64(Capacity) / 1024 / 1024 / 1024} Gb\n");
    richTextBox1.AppendText("*************************************\n");
}

WQL知识

有一个自带的 WQL 测试工具,叫 wbemtest.exe,win+R运行

先连接

命名空间:root\cimv2

后查询

查询 :Select * From Win32_LogicalDisk

双击查看详细

点击显示MOF,显示全部

内存

语法

[Dynamic, Provider("CIMWin32"), UUID("{FAF76B93-798C-11D2-AAD1-006008C78BC7}"), AMENDMENT]
class Win32_PhysicalMemory : CIM_PhysicalMemory
{
  uint32   Attributes;
  string   BankLabel;
  uint64   Capacity;
  string   Caption;
  uint32   ConfiguredClockSpeed;
  uint32   ConfiguredVoltage;
  string   CreationClassName;
  uint16   DataWidth;
  string   Description;
  string   DeviceLocator;
  uint16   FormFactor;
  boolean  HotSwappable;
  datetime InstallDate;
  uint16   InterleaveDataDepth;
  uint32   InterleavePosition;
  string   Manufacturer;
  uint32   MaxVoltage;
  uint16   MemoryType;
  uint32   MinVoltage;
  string   Model;
  string   Name;
  string   OtherIdentifyingInfo;
  string   PartNumber;
  uint32   PositionInRow;
  boolean  PoweredOn;
  boolean  Removable;
  boolean  Replaceable;
  string   SerialNumber;
  string   SKU;
  uint32   SMBIOSMemoryType;
  uint32   Speed;
  string   Status;
  string   Tag;
  uint16   TotalWidth;
  uint16   TypeDetail;
  string   Version;
};

powershell 示例

function get-WmiMemoryFormFactor {
param ([uint16] $char)
​
If ($char -ge 0 -and  $char  -le 22) {
​
switch ($char) {
0     {"00-Unknown"}
1     {"01-Other"}
2     {"02-SiP"}
3     {"03-DIP"}
4     {"04-ZIP"}
5     {"05-SOJ"}
6     {"06-Proprietary"}
7     {"07-SIMM"}
8     {"08-DIMM"}
9     {"09-TSOPO"}
10     {"10-PGA"}
11     {"11-RIM"}
12     {"12-SODIMM"}
13     {"13-SRIMM"}
14     {"14-SMD"}
15     {"15-SSMP"}
16     {"16-QFP"}
17     {"17-TQFP"}
18     {"18-SOIC"}
19     {"19-LCC"}
20     {"20-PLCC"}
21     {"21-FPGA"}
22     {"22-LGA"}
}
}
​
else {"{0} - undefined value" -f $char
}
​
Return
}
​
# Helper function to return memory Interleave  Position
​
function get-WmiInterleavePosition {
param ([uint32] $char)
​
If ($char -ge 0 -and  $char -le 2) {
​
switch ($char) {
0     {"00-Non-Interleaved"}
1     {"01-First Position"}
2     {"02-Second Position"}
}
}
​
else {"{0} - undefined value" -f $char
}
​
Return
}
​
​
# Helper function to return Memory Tupe
function get-WmiMemoryType {
param ([uint16] $char)
​
If ($char -ge 0 -and  $char  -le 20) {
​
switch ($char) {
0     {"00-Unknown"}
1     {"01-Other"}
2     {"02-DRAM"}
3     {"03-Synchronous DRAM"}
4     {"04-Cache DRAM"}
5     {"05-EDO"}
6     {"06-EDRAM"}
7     {"07-VRAM"}
8     {"08-SRAM"}
9     {"09-ROM"}
10     {"10-ROM"}
11     {"11-FLASH"}
12     {"12-EEPROM"}
13     {"13-FEPROM"}
14     {"14-EPROM"}
15     {"15-CDRAM"}
16     {"16-3DRAM"}
17     {"17-SDRAM"}
18     {"18-SGRAM"}
19     {"19-RDRAM"}
20     {"20-DDR"}
}
​
}
​
else {"{0} - undefined value" -f $char
}
​
Return
}
​
​
# Get the object
$memory = Get-WMIObject Win32_PhysicalMemory
​
#  Format and Print
"System has {0} memory sticks:" -f $memory.count
​
Foreach ($stick in $memory) {
​
# Do some conversions
$cap=$stick.capacity/1mb
$ff=get-WmiMemoryFormFactor($stick.FormFactor)
$ilp=get-WmiInterleavePosition($stick.InterleavePosition)
$mt=get-WMIMemoryType($stick.MemoryType)
​
# print details of each stick
"BankLabel            {0}"  -f $stick.banklabel
"Capacity (MB)        {0}"  -f $cap
"Caption              {0}"  -f $stick.Caption
"CreationClassName    {0}"  -f $stick.creationclassname
"DataWidth            {0}"  -f $stick.DataWidth
"Description          {0}"  -f $stick.Description
"DeviceLocator        {0}"  -f $stick.DeviceLocator
"FormFactor           {0}"  -f $ff
"HotSwappable         {0}"  -f $stick.HotSwappable
"InstallDate          {0}"  -f $stick.InstallDate
"InterleaveDataDepth  {0}"  -f $stick.InterleaveDataDepth
"InterleavePosition   {0}"  -f $ilp
"Manufacturer         {0}"  -f $stick.Manufacturer
"MemoryType           {0}"  -f $mt
"Model                {0}"  -f $stick.Model
"Name                 {0}"  -f $stick.Name
"OtherIdentifyingInfo {0}"  -f $stick.OtherIdentifyingInfo
"PartNumber           {0}"  -f $stick.PartNumber
"PositionInRow        {0}"  -f $stick.PositionInRow
"PoweredOn            {0}"  -f $stick.PoweredOn
"Removable            {0}"  -f $stick.Removable
"Replaceable          {0}"  -f $stick.Replaceable
"SerialNumber         {0}"  -f $stick.SerialNumber
"SKU                  {0}"  -f $stick.SKU 
"Speed                {0}"  -f $stick.Speed 
"Status               {0}"  -f $stick.Status
"Tag                  {0}"  -f $stick.Tag
"TotalWidth           {0}"  -f $stick.TotalWidth 
"TypeDetail           {0}"  -f $stick.TypeDetail
"Version              {0}"  -f $stick.Version
""
}
"-----"

示例输出:

BankLabel            BANK 0
Capacity (MB)        8192
Caption              物理内存
CreationClassName    Win32_PhysicalMemory
DataWidth            64
Description          物理内存
DeviceLocator        ChannelA-DIMM0
FormFactor           12-SODIMM
HotSwappable
InstallDate
InterleaveDataDepth  1
InterleavePosition   01-First Position
Manufacturer         8A76
MemoryType           00-Unknown
Model
Name                 物理内存
OtherIdentifyingInfo
PartNumber           LD4AS008G-H3200GST
PositionInRow
PoweredOn
Removable
Replaceable
SerialNumber         221200F5
SKU
Speed                3200
Status
Tag                  Physical Memory 0
TotalWidth           64
TypeDetail           128
Version
​
BankLabel            BANK 2
Capacity (MB)        8192
Caption              物理内存
CreationClassName    Win32_PhysicalMemory
DataWidth            64
Description          物理内存
DeviceLocator        ChannelB-DIMM0
FormFactor           12-SODIMM
HotSwappable
InstallDate
InterleaveDataDepth  1
InterleavePosition   02-Second Position
Manufacturer         8A76
MemoryType           00-Unknown
Model
Name                 物理内存
OtherIdentifyingInfo
PartNumber           LD4AS008G-H3200GST
PositionInRow
PoweredOn
Removable
Replaceable
SerialNumber         22120235
SKU
Speed                3200
Status
Tag                  Physical Memory 1
TotalWidth           64
TypeDetail           128
Version
​
-----

LICENSED UNDER CC BY-NC-SA 4.0
Comment