使用WMI枚举音频输入设备

使用WMI枚举音频输入设备

本文介绍了使用WMI枚举音频输入设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#项目中使用NAudio,并且我正在寻找一种枚举音频输入设备(麦克风等)的方法,因此我可以获得它们的全名(不仅是我可以输入的31个字符的长名称)从NAudio获取).我浏览了几篇文章,人们用WMI枚举音频输出设备:

ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
       "SELECT * FROM Win32_SoundDevice");

ManagementObjectCollection objCollection = objSearcher.Get();

是否也可以通过这种方式枚举输入设备?

谢谢

解决方案

要探索WMI查询,可以使用为您生成WMI代码的工具.您将有很多WMI管理类可以从中获取信息.您可以从Microsoft下载中心的此处下载该工具. /p>

几年前,我写了一篇博客帖子 WMI管理服务.希望这能给您一个良好的开端.

以下是该工具生成的摘要,用于获取设备上已安装的声卡的列表.

 public static void Main()
 {
     try
     {
         ManagementObjectSearcher searcher =
             new ManagementObjectSearcher("root\\CIMV2",
             "SELECT * FROM Win32_SoundDevice");

         foreach (ManagementObject queryObj in searcher.Get())
         {
             Console.WriteLine("-----------------------------------");
             Console.WriteLine("List of sound cards installed");
             Console.WriteLine("-----------------------------------");
             Console.WriteLine("ProductName: {0}", queryObj["ProductName"]);
             Console.WriteLine("Availability: {0}", queryObj["Availability"]);
             Console.WriteLine("Caption: {0}", queryObj["Caption"]);
             Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
             Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
             Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
             Console.WriteLine("Description: {0}", queryObj["Description"]);
             Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
             Console.WriteLine("DMABufferSize: {0}", queryObj["DMABufferSize"]);
             Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
             Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
             Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
             Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
             Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
             Console.WriteLine("MPU401Address: {0}", queryObj["MPU401Address"]);
             Console.WriteLine("Name: {0}", queryObj["Name"]);
             Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
             Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
             Console.WriteLine("Status: {0}", queryObj["Status"]);
             Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
             Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
             Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
         }
     }
     catch (ManagementException e)
     {
        Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
     }
 }

这是输出-

-----------------------------------
List of sound cards installed
-----------------------------------
ProductName: Realtek High Definition Audio
Availability:
Caption: Realtek High Definition Audio
ConfigManagerErrorCode: 0
ConfigManagerUserConfig: False
CreationClassName: Win32_SoundDevice
Description: Realtek High Definition Audio
DeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer: Realtek
MPU401Address:
Name: Realtek High Definition Audio
PNPDeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
PowerManagementSupported: False
Status: OK
StatusInfo: 3
SystemCreationClassName: Win32_ComputerSystem
SystemName: PC-2322Q1

I am using NAudio in my C# project, and I am looking for a way to enumerate audio input devices (microphone etc.), so i can get full name of them (not only the 31-characters long name that i can get from NAudio). I went through a few posts where people were enumerating audio output devices with WMI:

ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
       "SELECT * FROM Win32_SoundDevice");

ManagementObjectCollection objCollection = objSearcher.Get();

Is it possible to enumerate input devices this way as well?

Thanks

解决方案

To explore the WMI queries you can use a tool that generates the WMI code for you. You'll have plenty of WMI management classes to get the information from.You can download the tool from Microsoft download center here

I wrote a blog post few years back about using WMI management services for administration. Hope this would give you a head start.

Here's snippet generated from the tool to get the list of installed sound cards on the device.

 public static void Main()
 {
     try
     {
         ManagementObjectSearcher searcher =
             new ManagementObjectSearcher("root\\CIMV2",
             "SELECT * FROM Win32_SoundDevice");

         foreach (ManagementObject queryObj in searcher.Get())
         {
             Console.WriteLine("-----------------------------------");
             Console.WriteLine("List of sound cards installed");
             Console.WriteLine("-----------------------------------");
             Console.WriteLine("ProductName: {0}", queryObj["ProductName"]);
             Console.WriteLine("Availability: {0}", queryObj["Availability"]);
             Console.WriteLine("Caption: {0}", queryObj["Caption"]);
             Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
             Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
             Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
             Console.WriteLine("Description: {0}", queryObj["Description"]);
             Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
             Console.WriteLine("DMABufferSize: {0}", queryObj["DMABufferSize"]);
             Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
             Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
             Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
             Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
             Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
             Console.WriteLine("MPU401Address: {0}", queryObj["MPU401Address"]);
             Console.WriteLine("Name: {0}", queryObj["Name"]);
             Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
             Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
             Console.WriteLine("Status: {0}", queryObj["Status"]);
             Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
             Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
             Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
         }
     }
     catch (ManagementException e)
     {
        Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
     }
 }

Here's the output -

-----------------------------------
List of sound cards installed
-----------------------------------
ProductName: Realtek High Definition Audio
Availability:
Caption: Realtek High Definition Audio
ConfigManagerErrorCode: 0
ConfigManagerUserConfig: False
CreationClassName: Win32_SoundDevice
Description: Realtek High Definition Audio
DeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
DMABufferSize:
ErrorCleared:
ErrorDescription:
InstallDate:
LastErrorCode:
Manufacturer: Realtek
MPU401Address:
Name: Realtek High Definition Audio
PNPDeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_103C304A&REV_1001\4&3867FD9A&0&0001
PowerManagementSupported: False
Status: OK
StatusInfo: 3
SystemCreationClassName: Win32_ComputerSystem
SystemName: PC-2322Q1

这篇关于使用WMI枚举音频输入设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:57