Enumerate audio input devices with WMI

后端 未结 2 1375
难免孤独
难免孤独 2021-01-23 02:15

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 lon

相关标签:
2条回答
  • 2021-01-23 02:56

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

    0 讨论(0)
  • 2021-01-23 03:03

    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
    
    0 讨论(0)
提交回复
热议问题