通过VB.net获取VGA BUS类型

时间:2013-04-10 07:07:06

标签: vb.net pci bus vga pci-e

如何通过VB.net获取VGA BUS类型? 我需要一个源代码,在您的计算机中配有总线tpye的视频卡后写入运行。 (AGP,PCI,PCI-e ...)

抱歉我的英文不好!

2 个答案:

答案 0 :(得分:0)

您可以使用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

答案 1 :(得分:0)

不是最好的解决方案,但它返回哪个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