title: Get list of audio devices and select one using c#
body: <p>Hi I am creating a desktop based application in windows using C#.</p>
  
  <p>I have to show list of all available audio &amp; video devices in 2 different combo boxes.
  Selecting any device from combo box will set that particular device as the default one</p>
  
  <p>I am using WMI.</p>
  
  <p>Code to get list of available audio devices:</p>
  
  <pre><code>ManagementObjectSearcher mo = 
        new ManagementObjectSearcher("select * from Win32_SoundDevice");
  
  foreach (ManagementObject soundDevice in mo.Get())
  {
       String deviceId = soundDevice.GetPropertyValue("DeviceId").ToString();
       String name  = soundDevice.GetPropertyValue("Name").ToString();
  
    //saving the name  and device id in array
  } 
  </code></pre>
  
  <p>if i try to set the device like this:</p>
  
  <pre><code> using (RegistryKey audioDeviceKey = 
  Registry.LocalMachine.OpenSubKey(audioDevicesReg
     + @"\" + audioDeviceList.SelectedText.ToString(), true)){}
  </code></pre>
  
  <p>i get exception : </p>
  
  <pre><code>System.Security.SecurityException occurred in mscorlib.dll
  </code></pre>
  
  <p>Now I have few questions:</p>
  
  <pre><code>1) How to set the selected device as the default audio device?
  2) The array contains device name as : "High Definition audio device" 
  even when I have attached a headset.
  3) I want the list as speaker,headset etc...How to get that?
  </code></pre>
  
  <p>can anybody point me in the right direction?</p>