如何通过 VB.net 获取 VGA BUS 类型(AGP、PCI、PCI-e...)?
这将返回计算机中的视频卡:SELECT Name, PNPDeviceID FROM Win32_VideoController
如何从这些视频卡中获取总线类型以将 PCI 或 PCI-e 或 AGP 连接到计算机?
您可以使用 WMI 来获取此信息。我使用了下面的代码。您必须添加对 System.Management 的引用。这段代码很脆弱,但它表明可以使用 WMI 获得信息。查看 MSDN 上的文档,了解您可能感兴趣的其他 WMI 类。
Private Shared Sub Main()
Dim videoControllers As ManagementObjectCollection = getManagementObjects("Win32_VideoController")
For Each controllerObj As ManagementObject in videoControllers
Dim pnpDeviceID As String = Path.GetFileName(controllerObj.Properties("PNPDeviceID").Value.ToString())
Dim deviceBus As String = getDeviceBus(pnpDeviceID)
Dim busType As String = getBusType(deviceBus)
Console.WriteLine("{0}: {1}", controllerObj.Properties("Name").Value, busType)
Next
End Sub
Private Shared Function getManagementObjects(ByVal wmiClass As String) As ManagementObjectCollection
Using searcher As ManagementObjectSearcher = New ManagementObjectSearcher(String.Format("select * from {0}", wmiClass))
Return searcher.Get()
End Using
End Function
Private Shared Function getDeviceBus(ByVal pnpDeviceID As String) As String
Dim result As String = Nothing
Dim coll As ManagementObjectCollection = getManagementObjects("Win32_DeviceBus")
For Each mobj As ManagementObject In coll
For Each props As PropertyData in mobj.Properties
If props.Name = "Dependent" AndAlso props.Value.ToString().Contains(pnpDeviceID) Then
result = mobj.Properties("Antecedent").Value.ToString().Split("="c)(1).Replace("""", "")
Exit For
End If
Next
Next
Return result
End Function
Private Shared Function getBusType(ByVal deviceBus As String) As String
Dim busTypes As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
busTypes.Add(-1, "Undefined")
busTypes.Add(0, "Internal")
busTypes.Add(1, "ISA")
busTypes.Add(2, "EISA")
busTypes.Add(3, "MicroChannel")
busTypes.Add(4, "TurboChannel")
busTypes.Add(5, "PCI Bus")
busTypes.Add(6, "VME Bus")
busTypes.Add(7, "NuBus")
busTypes.Add(8, "PCMCIA Bus")
busTypes.Add(9, "C Bus")
busTypes.Add(10, "MPI Bus")
busTypes.Add(11, "MPSA Bus")
busTypes.Add(12, "Internal Processor")
busTypes.Add(13, "Internal Power Bus")
busTypes.Add(14, "PNP ISA Bus")
busTypes.Add(15, "PNP Bus")
busTypes.Add(16, "Maximum Interface Type")
Dim result As String = Nothing
Dim coll As ManagementObjectCollection = getManagementObjects("Win32_Bus")
Dim busType As Integer = -1
For Each mobj As ManagementObject in coll
If mobj.Properties("DeviceID").Value.ToString() = deviceBus Then
Integer.TryParse(mobj.Properties("BusType").Value.ToString(), busType)
Exit For
End If
Next
result = busTypes(busType)
Return result
End Function
这在我的盒子上产生了这个结果:
ConfigMgr Remote Control Driver: PCI Bus
NVIDIA GeForce 8400 GS : PCI Bus
Winvnc video hook driver: PCI Bus
不是最好的解决方案,但它会返回哪个VGA在哪个端口,显示是PCI还是PCIe,我也认为是AGP,但我无法测试这个,因为我不再有AGP主板。
Function GetName(ByVal devid As String) As String
Dim sql As String = "SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE """ & devid.Replace("\", "\\") & """"
Dim videoControllers As New ManagementObjectSearcher("root\CIMV2", sql)
For Each controllerObj As ManagementObject In videoControllers.Get()
Return controllerObj.Properties("Name").Value.ToString()
Next
Return devid
End Function
Function GetParent(ByVal devid As String) As String
Dim sql As String = "SELECT * FROM Win32_PnPEntity WHERE DeviceID = """ & devid.Replace("\", "\\") & """"
Dim MObjS As New ManagementObjectSearcher("root\CIMV2", sql)
For Each MObj As ManagementObject In MObjS.Get()
Dim outParams As ManagementBaseObject = MObj.InvokeMethod("GetDeviceProperties", Nothing, Nothing)
For Each r In outParams.Properties("deviceProperties").Value
If r.Properties("KeyName").Value = "DEVPKEY_Device_Parent" Then
Return GetName(r.Properties("Data").Value)
End If
Next
Next
Return devid
End Function
Sub GetGPUs()
For Each MObj As ManagementObject In getManagementObjects("Win32_VideoController")
Dim name As String = MObj.Properties("Name").Value.ToString()
Dim PNPDeviceID As String = MObj.Properties("PNPDeviceID").Value.ToString()
Debug.WriteLine(name & " - " & GetParent(PNPDeviceID))
Next
End Sub
结果是:
NVIDIA GeForce GTX 1060 6GB - Intel(R) Xeon(R) E3 - 1200/1500 v5/6th Gen Intel(R) Core(TM) PCIe Controller (x16) - 1901