【发布时间】:2014-09-27 22:29:42
【问题描述】:
我正在编写一个使用图形卡验证的程序。 我尝试使用多种方式;我找到的最接近的是使用:
lblGrapics.Text = infotypes.VideocardName.GetName()
但自动返回等于 1。 如何获取卡名和其他规格?
【问题讨论】:
我正在编写一个使用图形卡验证的程序。 我尝试使用多种方式;我找到的最接近的是使用:
lblGrapics.Text = infotypes.VideocardName.GetName()
但自动返回等于 1。 如何获取卡名和其他规格?
【问题讨论】:
这将允许您轮询任何 WMI 类并获取所需的属性值。在您的情况下,您可以从the Win32_VideoController class 中进行选择。其他 WMI 类可以是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 类:
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
该类包括一种转储类的属性名称和值的方法。要使用它:
WMI.DebugWMIPropValues("Win32_VideoController")
只需在输出窗口中查看结果(调试菜单 -> Windows -> 输出)
购物清单的示例输出:
Item: AdapterRAM value: 1073741824
Item: Name value: AMD Radeon HD 6450
Item: VideoProcessor value: ATI display adapter (0x6779)
在我的系统上工作TM
注意,更新:GetWMISettingsDictionary 用于获取单个项目的属性。照原样,它会得到大多数东西的设置,但只有第一个视频卡,第一个显示器等。
有几种方法可以根据您的需要进行更改。可以对其进行修改以在List 中为每个项目返回一个单独的Dictionary。或者,您可以将 WHERE 子句附加到 WMI 类名称以获取特定设备的属性并根据需要经常调用它:
wmiClass = "Win32_VideoController WHERE Name = 'FizzBar Deluxe'"
' or
wmiClass = "Win32_VideoController WHERE DeviceID = 'VideoController1'"
wmiItems = WMI.GetWMISettingsDictionary(wmiClass , shopList)
名称搜索现在不区分大小写。
最后,请注意,对于低端视频适配器,AdapterRAM 将报告总系统 RAM。这是因为没有任何板载 RAM 的适配器仅使用系统 RAM,因此它可以正确报告。
【讨论】:
可以使用 WMI 获取显卡信息。您需要引用 System.Management 并导入它。
WMI is a great library which contains the details about various components required for the system to operate. Hard Disk Drive related information, processor information, Network components and the list goes on. It is really easy to query the data if you know a little about the data how it is organized.
您必须使用ManagementObjectSearcher 类。
例子:
Imports System.Management
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(GetGraphicsCardName())
End Sub
Private Function GetGraphicsCardName() As String
Dim GraphicsCardName = String.Empty
Try
Dim WmiSelect As New ManagementObjectSearcher _("rootCIMV2", "SELECT * FROM Win32_VideoController")
For Each WmiResults As ManagementObject In WmiSelect.Get()
GraphicsCardName = WmiResults.GetPropertyValue("Name").ToString
If (Not String.IsNullOrEmpty(GraphicsCardName)) Then
Exit For
End If
Next
Catch err As ManagementException
MessageBox.Show(err.Message)
End Try
Return GraphicsCardName
End Function
End Class
【讨论】:
System.Management的引用吗?
“invalid namespace”不是System.Management,是因为
的第一个参数Dim WmiSelect As New ManagementObjectSearcher _("rootCIMV2", "SELECT * FROM
不接受。
尝试使用另一个没有第一个参数的构造函数:
Dim WmiSelect As New ManagementObjectSearcher _("SELECT * FROM Win32_VideoController")
【讨论】: