使用C#或VB访问设备信息

时间:2017-01-21 17:25:30

标签: c# vb.net winapi

如果我在Windows上打开设备管理器,则转到"端口(COM LTP)"我看到7个设备。 1 - 内置计算机RS323 2-6- USB串行端口(COM X)

如果我点击右键 - >属性 - >细节,我可以看到一大堆价值观。 对我有意思的是#34;地址"和"硬件ID"这是" FTDIBUS \ COMPORT& VID_0403& PID_6001"

如何使用C#或更好的VB访问此信息? 我试过了

var win32DeviceClassName = "Win32_SerialPort";
var query = string.Format("select * from {0}", win32DeviceClassName);

然后为每个属性创建一个控制台打印,但只有内置的COM1显示信息

P.S。我需要这个信息,找出哪个地址有哪个com-port然后将comport更改为所需的。

2 个答案:

答案 0 :(得分:0)

试试这个:

    Try
        Using mos As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=""{4d36e978-e325-11ce-bfc1-08002be10318}""")
            Dim AvailableComPorts = SerialPort.GetPortNames().ToList()
            Dim q As ManagementObjectCollection = mos.Get()

            For Each x As ManagementObject In q
                Console.WriteLine(x.Properties("Name").Value)
                Console.WriteLine(x.Properties("DeviceID").Value)
            Next
        End Using
    Catch ex As Exception
        Throw
    End Try

答案 1 :(得分:0)

以下函数返回包含名称和所有可用属性的串行端口属性列表的列表。我添加了一个可选的重载“ShowNullProperties”,如果设置为TRUE将返回所有属性,无论该值是否为null。 “Caption”和“DeviceID”的属性在列表末尾再次手动添加,这样我可以在返回列表时轻松识别端口名称和设备ID,而无需搜索整个列表。要使下面的代码工作,您需要在设计器中使用名为trv_ports的树视图和图像列表,但是您可以注释掉图像列表代码并显示一个?用于图像图标。

stripe.Key = "sk_test_################"

customerParams := &stripe.CustomerParams{
    Desc:          "Customer for xyz.com",
    Email:         "test@email.com",
}

err := customerParams.SetSource("tok_####################")
if err != nil {
    return nil, err
}

cus, err := customer.New(customerParams)
if err != nil {
    return nil, err
}

// Charge the user's card
cp := &stripe.ChargeParams{
    Amount:   100,
    Currency: "usd",
    Desc:     "some description",
    Customer: cus.ID,
}

err = charge.New(cp)

然后在Form1 Load事件中调用此函数并填充树视图。

Imports System.Management

    Private Function Get_ListofSerialPorts(Optional ByVal ShowNullProperties As Boolean = False) As List(Of List(Of String))

    ' This function returns a list of serial port property lists 

    Dim RtnList As New List(Of List(Of String))

    Dim portSearcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'")

    For Each port As System.Management.ManagementObject In portSearcher.Get()
        Dim NewList As New List(Of String)

        For Each prop As PropertyData In port.Properties

            If ShowNullProperties = True Then
                ' Show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString) Else NewList.Add(prop.Name.ToString & ": " & "Nothing")
            Else
                ' Do not show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString)
            End If

        Next
        ' Add these two properties on the end to use later for the name and device ID fields 
        NewList.Add(port("Caption").ToString)
        NewList.Add(port("DeviceID").ToString)

        RtnList.Add(NewList)

    Next
    Return RtnList

End Function

使用ShowNullProperties = True输出:(如果设置为False,则显示“Nothing”的所有值都不会添加到列表中)

enter image description here