Enumerate audio input devices with WMI

怎甘沉沦 提交于 2019-11-28 09:51:51

问题


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


回答1:


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



回答2:


These are sound devices, so it includes input and output devices. Soundcards can have 0 or more outputs and 0 or more inputs.



来源:https://stackoverflow.com/questions/36159760/enumerate-audio-input-devices-with-wmi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!