111,097
社区成员




public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{
List<string> strs = new List<string>();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
{
strs.Add(hardInfo.Properties[propKey].Value.ToString());
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch
{
return null;
}
finally
{
strs = null;
}
}
private void getPortDeviceName()
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher
("select * from Win32_PnPEntity where Name like '%(COM%'"))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties["Name"].Value != null)
{
string deviceName = hardInfo.Properties["Name"].Value.ToString();
Console.WriteLine(deviceName);
}
}
}
}