使用Microsoft.Management.Infrastructure检索串行端口信息



为了获得有关串行端口设备的信息,使用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实现这一点,我还没有找到示例,文档也不够详细。

非常相似:

  • 创建CimSession,在其中指定ComputerName(null表示LocalHost(和CimCredential对象(需要传递通常的用户名和密码进行身份验证(。这反映了System Management的ConnectionOption
  • 使用CimSession的QueryInstances方法来构建查询,传递命名空间和类。这反映了System.Management的SelectQuery和ManagementObjectSearcher
  • CCD_ 5返回一个IEnumerable<CimInstance>对象(好消息是这里没有返回COM对象(
  • 获取您关心的CimInstanceProperties的值

注意,您正在跳过WMI查询中的ConnectionOption和EnumerationOptions,这在性能方面并不是很好。

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

using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using (var session = CimSession.Create(null) { 
var ports = session.QueryInstances(@"rootcimv2", "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(@"rootcimv2", "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*(。

最新更新