public static string GetSerialPort()
{
return MulGetHardwareInfo(HardwareEnum.Win32_SerialPort, "Name");
}
//枚举win32 api
public enum HardwareEnum
{
Win32_SerialPort
,
Win32_SerialPortConfiguration
,
Win32_SerialPortSetting
}
public static string MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{
string strs = "";
string ttt = "STMicr";
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType);
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties[propKey] != null)
{
strs = hardInfo.Properties[propKey].Value.ToString();
string id = Convert.ToString(strs.Substring(0, 6));
if (id == ttt)
{
strs = Convert.ToString(strs.Substring(37, 5));
}
else
{
strs = "No Found";
}
}
}
return strs;
}