我正在编写一个使用图形卡验证的程序。 我尝试过多种方式;我找到的最接近的是:
lblGrapics.Text = infotypes.VideocardName.GetName()
但自动返回等于1。 我怎样才能获得卡片名称和其他说明?
答案 0 :(得分:6)
这将允许您轮询任何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
用于收集单个项目的属性。它会得到大多数东西的设置,但只有第一个显卡,第一个显示器等。
根据您的需要,有几种方法可以改变这种情况。可以修改它以在Dictionary
中为每个项目返回单独的List
。或者,您可以将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,因此报告正确。
答案 1 :(得分:1)
可以使用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
答案 2 :(得分:1)
“invalid namespace”与System.Management无关,它是因为
的第一个参数Dim WmiSelect As New ManagementObjectSearcher _("rootCIMV2", "SELECT * FROM
不被接受。
尝试使用没有第一个参数的其他构造函数:
Dim WmiSelect As New ManagementObjectSearcher _("SELECT * FROM Win32_VideoController")