获取硬件信息,例如显卡功能 [英] Get hardware information such as Graphic Card capabilities
问题描述
我正在编写一个使用图形卡验证的程序.我尝试使用多种方式;我发现的最接近的一个正在使用:
I'm writing a program that is using a validation of graphic card. I tried using multiple ways; the closest one i found was using:
lblGrapics.Text = infotypes.VideocardName.GetName()
但自动返回等于1.我如何获得卡名和其他规格?
but the automatic return is equals 1. how can i get the card name and other specifations?
推荐答案
这将允许您轮询任何 WMI 类并获得所需的属性值.在您的情况下,您可以从 Win32_VideoController 类.其他 WMI 类可以在此处找到.
This will allow you to poll any WMI class and get the desired values for the properties. In your case, you would select from the Win32_VideoController class. Other WMI class can be found here.
Imports System.Management
Public Class WMI
Public Shared Function GetWMISettingsDictionary(ByVal wmiClass As String,
ShoppingList() As String) As Dictionary(Of String, String)
Dim wmiInfo As New Dictionary(Of String, String)
Dim searcher As New System.Management.ManagementObjectSearcher("select * from " & wmiClass)
For Each item As System.Management.ManagementObject In searcher.Get
For Each PC As System.Management.PropertyData In item.Properties
' perform case insensitive search
For Each s As String in ShoppingList
If s.ToLowerInvariant = PC.Name.ToLowerInvariant Then
If PC.Value IsNot Nothing Then
wmiInfo.Add(PC.Name, PC.Value.ToString)
' halt search-by-name
Exit For
End If
End If
Next
Next
' Note: this is to prevent a crash when there is more than one item
' WMI reports on such as 2 display adapters; just get the first one.
Exit For
Next
Return wmiInfo
End Function
' helpful tool to see how WMI props are organized, discover the names etc
Public Shared Sub DebugWMIPropValues(wmiClass As String)
Using searcher As New Management.ManagementObjectSearcher("Select * from " & wmiClass)
Dim moReturn As Management.ManagementObjectCollection = searcher.Get
For Each mo As Management.ManagementObject In moReturn
Console.WriteLine("====")
DebugProperties(mo)
Next
End Using
End Sub
' debug tool to poll a management object to get the properties and values
Private Shared Sub DebugProperties(mo As Management.ManagementObject)
For Each pd As PropertyData In mo.Properties
If pd.Value IsNot Nothing Then
If pd.Value.GetType Is GetType(String()) Then
Dim n As Integer = 0
For Each s As String In CType(pd.Value, Array)
Console.WriteLine("{0}({1}): {2}", pd.Name, n.ToString,
If(pd.Value IsNot Nothing,
s,
"Nothing"))
n += 1
Next
Else
Console.WriteLine("{0}: {1}", pd.Name,
If(pd.Value IsNot Nothing,
pd.Value.ToString,
"Nothing"))
End If
End If
Next
End Sub
End Class
要使用它,您只需创建所需属性的购物清单"并传递 WMI 类:
To use it you just create a "shopping list" of the properties you want and pass the WMI Class:
Dim shopList() As String = {"VideoProcessor", "Name", "AdapterRAM"}
' the return is a Dictionary to keep the Name and Value together
Dim wmiItems As Dictionary(Of String, String)
wmiItems = WMI.GetWMISettingsDictionary("Win32_VideoController", shopList)
' print them to the console window:
For Each kvp As KeyValuePair(Of String, String) In wmiItems
Console.WriteLine("Item: {0} value: {1}", kvp.Key, kvp.Value)
Next
该类包括一种转储类的属性名称和值的方法.使用它:
The class includes a way to dump the property names and values for a class. To use it:
WMI.DebugWMIPropValues("Win32_VideoController")
只需在输出窗口中查看结果(调试菜单 -> Windows -> 输出)
Just look in the Output window for the results (Debug menu -> Windows -> Ouput)
购物清单的示例输出:
Item: AdapterRAM value: 1073741824
Item: Name value: AMD Radeon HD 6450
Item: VideoProcessor value: ATI display adapter (0x6779)
适用于我的系统TM
注释,更新:GetWMISettingsDictionary
用于收集单个项目的属性.按原样,它将获得大多数设置,但只有第一个视频卡、第一个显示器等.
Notes, Update: GetWMISettingsDictionary
is intended for harvesting the properties for a single item. As is, it will get the settings for most things, but only the first video card, the first display etc.
根据您的需要,有多种方法可以更改此设置.可以修改它以在 List
中为每个项目返回一个单独的 Dictionary
.或者您可以将 WHERE 子句附加到 WMI 类名称以获取特定设备的属性并根据需要经常调用它:
There are several ways to change this depending on what you need. It could be modified to return a separate Dictionary
in a List
for each item. Or you could append a WHERE clause to the WMI class name to get the properties for a specific device and call it as often as needed:
wmiClass = "Win32_VideoController WHERE Name = 'FizzBar Deluxe'"
' or
wmiClass = "Win32_VideoController WHERE DeviceID = 'VideoController1'"
wmiItems = WMI.GetWMISettingsDictionary(wmiClass , shopList)
名称搜索现在不区分大小写.
The name search is now case-insensitive.
最后,请注意对于低端视频适配器,AdapterRAM
将报告总系统 RAM.这是因为没有任何板载 RAM 的适配器只使用系统 RAM,因此报告正确.
Finally, note that with low-end video adapters, AdapterRAM
will report total System RAM. This is because adapters without any on-board RAM simply use system RAM, so it is reporting correctly.
这篇关于获取硬件信息,例如显卡功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!