我正在编写一个使用图形卡验证的程序.我尝试过多种方式; 我找到的最接近的是使用:
lblGrapics.Text = infotypes.VideocardName.GetName()
Run Code Online (Sandbox Code Playgroud)
但自动返回等于1.我怎样才能获得卡名和其他说明?
这将允许您轮询任何WMI类并获取属性的所需值.在您的情况下,您将从Win32_VideoController类中进行选择.其他WMI类可以在这里找到.
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
Run Code Online (Sandbox Code Playgroud)
要使用它,您只需创建所需属性的"购物清单"并传递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
Run Code Online (Sandbox Code Playgroud)
该类包括转储类的属性名称和值的方法.要使用它:
WMI.DebugWMIPropValues("Win32_VideoController")
Run Code Online (Sandbox Code Playgroud)
只需在Output窗口中查看结果(Debug菜单 - > Windows - > Ouput)
购物清单的示例输出:
Item: AdapterRAM value: 1073741824
Item: Name value: AMD Radeon HD 6450
Item: VideoProcessor value: ATI display adapter (0x6779)
Run Code Online (Sandbox Code Playgroud)
适用于My System TM
注释,更新:GetWMISettingsDictionary用于收集单个项目的属性.它会得到大多数东西的设置,但只有第一个显卡,第一个显示器等.
有几种方法可以根据您的需要进行更改.可以修改它以Dictionary在List每个项目中为a 返回单独的内容.或者,您可以将WHERE子句附加到WMI类名称以获取特定设备的属性,并根据需要经常调用它:
wmiClass = "Win32_VideoController WHERE Name = 'FizzBar Deluxe'"
' or
wmiClass = "Win32_VideoController WHERE DeviceID = 'VideoController1'"
wmiItems = WMI.GetWMISettingsDictionary(wmiClass , shopList)
Run Code Online (Sandbox Code Playgroud)
名称搜索现在不区分大小写.
最后,请注意,使用低端视频适配器,AdapterRAM将报告总系统RAM.这是因为没有任何板载RAM的适配器只使用系统RAM,因此报告正确.
| 归档时间: |
|
| 查看次数: |
4742 次 |
| 最近记录: |