.net - WmiMonitorID - 将结果转换为 ASCII

标签 .net vb.net wmi wmi-query

正在尝试查询显示器的序列号和型号。
我设法使用 WMI 代码创建器生成以下代码:

Try
    Dim MInfo As New ManagementObjectSearcher("root\WMI", "SELECT * FROM WmiMonitorID")

    For Each Monitor In MInfo.Get()
        If Monitor("SerialNumberID") Is Nothing Then
            MsgBox("NA")
        Else
            Dim arrSerialNumberID As UInt16()
            arrSerialNumberID = Monitor("SerialNumberID")
            For Each arrValue As UInt16 In arrSerialNumberID
                Console.WriteLine("Serial: " & arrValue)
            Next
        End If
        If Monitor("UserFriendlyName") Is Nothing Then
            MsgBox("NA")
        Else
            Dim arrSerialNumberID As UInt16()
            arrSerialNumberID = Monitor("UserFriendlyName")
            For Each arrValue As UInt16 In arrSerialNumberID
                Console.WriteLine("Model: " & arrValue)
            Next
        End If
    Next
Catch err As ManagementException
    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
 End Try

输出如下:

SerialNumberID: 67,78,75,52,49,49,49,49,55,57,0,0,0,0,0,0    
UserFriendlyName: 104,112,32,76,49,53,51,48,0,0,0,0,0

我正在尝试将输出转换为 ASCII。

最佳答案

返回的数组需要转换为字符串,以变得对人眼友好。
UInt16 字节数组可以用 Convert.ToByte(UInt16) 转换, 然后用 Encoding.UTF8.GetString() 转换成字符串.

这里,一个转换方法包含在一个专门的类(MonitorID)中,用于返回系统中所有监视器的一些信息,查询\WMI\WmiMonitorID .
它可以用 \CIMV2\Win32_DesktopMonitor 扩展

你可以这样使用它:

Dim MonitorIds As List(Of MonitorID) = GetDesktopMonitorsID()

将返回(以及其他详细信息):

MonitorIds(0).SerialNumberID = CNK4111179  
MonitorIds(0).UserFriendlyName = HP L1530  

类和WMI查询方法:

Imports System.Management

Public Class MonitorID
    Public Property Active As Boolean?
    Public Property InstanceName As String
    Public Property ManufacturerName As String
    Public Property ProductCodeID As String
    Public Property SerialNumberID As String
    Public Property UserFriendlyName As String
    Public Property WeekOfManufacture As String
    Public Property YearOfManufacture As String

    Public Shared Function ConvertToString(mObject As Object) As String
        If mObject Is Nothing Then Return String.Empty
        Return Encoding.UTF8.GetString(CType(mObject, UInt16()).
            TakeWhile(Function(ui) ui <> 0).Select(Function(ui) Convert.ToByte(ui)).ToArray())
    End Function
End Class


Public Shared Function GetDesktopMonitorsID() As List(Of MonitorID)
    Dim MonitorsIds As New List(Of MonitorID)()

    Dim ConnOptions As New ConnectionOptions() With {
        .EnablePrivileges = True,
        .Timeout = EnumerationOptions.InfiniteTimeout
    }

    Dim mOptions As New EnumerationOptions() With {
        .Rewindable = False,
        .ReturnImmediately = True,
        .DirectRead = True,
        .EnumerateDeep = False
    }

    Dim mQuery As New SelectQuery("SELECT * FROM WmiMonitorID")
    Dim mScope As New ManagementScope($"\\{Environment.MachineName}\root\WMI", ConnOptions)
    mScope.Connect()

    Using moSearcher As New ManagementObjectSearcher(mScope, mQuery, mOptions)
        For Each moMonitor As ManagementObject In moSearcher.[Get]()

        MonitorsIds.Add(New MonitorID() With {
            .Active = CType(moMonitor("Active"), Boolean?),
            .InstanceName = moMonitor("InstanceName")?.ToString(),
            .ManufacturerName = MonitorID.ConvertToString(moMonitor("ManufacturerName")),
            .ProductCodeID = MonitorID.ConvertToString(moMonitor("ProductCodeID")),
            .SerialNumberID = MonitorID.ConvertToString(moMonitor("SerialNumberID")),
            .UserFriendlyName = MonitorID.ConvertToString(moMonitor("UserFriendlyName")),
            .WeekOfManufacture = moMonitor("WeekOfManufacture")?.ToString(),
            .YearOfManufacture = moMonitor("YearOfManufacture")?.ToString()
        })
        Next
    End Using
    Return MonitorsIds
End Function

关于.net - WmiMonitorID - 将结果转换为 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666719/

相关文章:

c# - 正确使用 .NET 并发集合

vb.net - DateTime 作为字典中的对象

windows - 使用 realbasic 获取 Windows 上的 CPU 数量

windows - 有没有办法以编程方式将启动脚本添加到本地组策略?

.net - MEF 在其他组件中找不到零件

.net - 您能计算出 Active Directory 使用的密码哈希吗?

.net - 如何将字节数组转换为位图实例 (.NET)?

c# - 使用 Telerik 导出到 Excel (XLSX) 时如何解释集合?

c++ - 如何在c中使用IWbemContext的 "__ProviderArchitecture"标志?

c# - 将文本框文本保存到 XML 文件中