.net 获取声卡基本信息
通过 ManagementObjectSearcher 类查询 Win32_SoundDevice 信息,获取 PNPDeviceID 信息
/// <summary>
/// 声卡PNPDeviceID
/// </summary>
public string GetSoundPNPID()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
foreach (ManagementObject mo in mos.Get())
{
st = mo["PNPDeviceID"].ToString();
}
return st;
}
运行结果:
获取声卡列表信息
通过ManagementObjectSearcher 类,初始化root\\CIMV2, 查询 Win32_SoundDevice 信息
遍历获取Name, Manufacturer , ProductName 值
/// <summary>
/// 获取声卡
/// </summary>
/// <returns></returns>
public static string GetSoundAdapters()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SoundDevice");
StringBuilder stringBuilder = new StringBuilder();
uint i = 1;
foreach (ManagementObject wmi in searcher.Get())
{
var n = wmi.GetPropertyValue("Name")?.ToString().Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©");
var m = wmi.GetPropertyValue("Manufacturer")?.ToString();
var pn = wmi.GetPropertyValue("ProductName")?.ToString();
stringBuilder.AppendLine($"序号{i}>>{n},{m}");
stringBuilder.AppendLine();
i++;
}
return stringBuilder.ToString();
}
运行结果: