0

为了获取有关串行端口设备的信息System.Management,我们可以按照获取串行端口信息中的说明进行操作:

using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.IO.Ports;        

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var searcher = new ManagementObjectSearcher
                ("SELECT * FROM WIN32_SerialPort"))
            {
                string[] portnames = SerialPort.GetPortNames();
                var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
                var tList = (from n in portnames
                            join p in ports on n equals p["DeviceID"].ToString()
                            select n + " - " + p["Caption"]).ToList();

                tList.ForEach(Console.WriteLine);
            }

            // pause program execution to review results...
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }
}

如何使用 来实现这一点Microsoft.Management.Infrastructure,我还没有找到示例并且文档不够详细。

4

1 回答 1

0

它非常相似:

请注意,您在 WMI 查询中跳过了ConnectionOptionEnumerationOptions,这在 PERF 方面并不是很好。

然后可以将您的查询翻译为:

using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;

using (var session = CimSession.Create(null) { 
    var ports = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM WIN32_SerialPort");
    string[] portnames = SerialPort.GetPortNames();

    var tList = (from n in portnames
                join p in ports on n equals p.CimInstanceProperties["DeviceID"].Value
                select n + " - " + p.CimInstanceProperties["Caption"].Value);
}

我不确定你为什么在string[] portnames = SerialPort.GetPortNames();这里使用。
您可以只使用 CimProperties:

using (var session = CimSession.Create(null)) {
    var ports = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM WIN32_SerialPort");

    var portsDescriptions = ports.Select(p =>
        $"{p.CimInstanceProperties["DeviceID"].Value} - {p.CimInstanceProperties["Caption"].Value}");

    // If you actually need to materialize a List<T>...
    portsDescriptions.ToList().ForEach(Console.WriteLine);
}

不相关,但可能有用:我建议构建一些方法来创建具有更多选项的 CimSession。例如:

public static CimSession CreateSession(string computerName) 
    => CreateSession(computerName, string.Empty, string.Empty, null);

public static CimSession CreateSession(string computerName, string domain, string userName, SecureString password)
{
    if (string.IsNullOrEmpty(computerName) || 
        computerName.Equals("localhost", StringComparison.InvariantCultureIgnoreCase)) {
        computerName = null;
    }
    var option = new CimSessionOptions();
    if (password != null && password.Length > 0) {
        option.AddDestinationCredentials(
            new CimCredential(PasswordAuthenticationMechanism.Default, domain, userName, password));
    }
    return CimSession.Create(computerName, option);
}

所以,而不是:

var session = CimSession.Create(null);

您可以将其称为:

// LocalHost, default permissions
var session = CreateSession(null); 

如果需要,或者传递域、用户名和密码(作为 Char*)。

于 2021-05-30T12:47:14.663 回答