这里的想法是能够找到在运行时连接的USB串口设备,从而不知道它的端口号,并在应用程序中使用它从设备中检索信息。
string comportInfo = string.Empty;
using (var entitySearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%" + SerialPortToFind + "%'"))
{
using (var serialPortSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"))
{
//testBase.SamplerWithCancel is like a for loop with exception control, a time between interations and amount of iterations to be made. It expects a true or false value to determine whether a desired condition is met. Failing to return a true value in the specified iterations throws an exception.
testBase.SamplerWithCancel(() =>
{
var portList = serialPortSearcher.Get().Cast<ManagementBaseObject>().ToList();
var matchingEntities = entitySearcher.Get().Cast<ManagementBaseObject>().First();
if (portList.Count != 0 && matchingEntities != null)
{
foreach (ManagementBaseObject port in portList)
{
if (port["InstanceName"].ToString().Contains(matchingEntities["DeviceID"].ToString()))
{
comportInfo = port["PortName"].ToString();
}
}
return true;
}
else
return false;
}, "Serial port not found", 3, 1500, 500, false, false);
}
}代码运行良好,但我想知道在哪里可以改进它,使其更有弹性,更不易出错。
我觉得使用LINQ比我所做的要好得多。
发布于 2015-11-19 10:16:10
代码运行良好,但我想知道在哪里可以改进它,使其更有弹性,更不易出错。
{},尽管它们可能是可选的。using块,您可以节省一些水平间距。if (portList.Count != 0 && matchingEntities != null)的条件,您可以提前返回,else将是冗余的,并且可以删除,这也节省了一些间距。matchingEntities与调用First()的结果不匹配。matchingEntities["DeviceID"].ToString()的结果存储到变量中将加快速度。实施上述各点将导致
string comportInfo = string.Empty;
using (var entitySearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%" + SerialPortToFind + "%'"))
using (var serialPortSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"))
{
//testBase.SamplerWithCancel is like a for loop with exception control, a time between interations and amount of iterations to be made. It expects a true or false value to determine whether a desired condition is met. Failing to return a true value in the specified iterations throws an exception.
testBase.SamplerWithCancel(() =>
{
var portList = serialPortSearcher.Get().Cast<ManagementBaseObject>().ToList();
var matchingEntity = entitySearcher.Get().Cast<ManagementBaseObject>().First();
if (portList.Count == 0 || matchingEntity == null)
{
return false;
}
string entity = matchingEntity["DeviceID"].ToString();
foreach (ManagementBaseObject port in portList)
{
if (port["InstanceName"].ToString().Contains(entity))
{
comportInfo = port["PortName"].ToString();
}
}
return true;
}, "Serial port not found", 3, 1500, 500, false, false);
}发布于 2015-11-20 08:16:39
我使用LINQ而不是foreach循环开发了一个迭代,同时还实现了Heslacher建议的改进,代码现在如下所示:
string comportInfo = string.Empty;
using (var entitySearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption LIKE '%" + SerialPortToFind + "%'"))
using (var serialPortSearcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName"))
{
testBase.SamplerWithCancel(() =>
{
var portList = serialPortSearcher.Get().Cast<ManagementBaseObject>().ToList();
var matchingEntity = entitySearcher.Get().Cast<ManagementBaseObject>().First();
if (portList.Count == 0 || matchingEntity == null)
{
return false;
}
var entity = matchingEntity["DeviceID"].ToString();
comportInfo = portList.Select(x => x)
.Where<ManagementBaseObject>(x => x["InstanceName"].ToString().Contains(entity))
.First()["PortName"].ToString();
return true;
}, "Device not found", 3, 1500, 500, false, false);
}https://codereview.stackexchange.com/questions/111178
复制相似问题