Title: SetInputToDefaultAudioDevice throws 'System.InvalidOperationException' occurred in Microsoft.Speech.dll
Tags: c#;speech-recognition;microsoft-speech-api
Question: Executing this code on Windows 10 Pro with microphone enabled throws an exception. 

Any idea why?

```using Microsoft.Speech.Recognition;
...
    static void Main(string[] args)
    {
        // Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale.
        using (
        SpeechRecognitionEngine recognizer =
          new SpeechRecognitionEngine(
            new System.Globalization.CultureInfo("en-GB")))
        {

            // Create a grammar for finding services in different cities.
            Choices locations = new Choices(new string[] { "office" });
            Choices devices = new Choices(new string[] { "lights" ,"shades"});
            Choices actions = new Choices(new string[] { "off" , "on", "up", "down"});

            GrammarBuilder findServices = new GrammarBuilder("Jarvis");
            findServices.Append(locations);
            findServices.Append(devices);
            findServices.Append(actions);

            // Create a Grammar object from the GrammarBuilder and load it to the recognizer.
            Grammar servicesGrammar = new Grammar(findServices);
            recognizer.LoadGrammarAsync(servicesGrammar);

            // Add a handler for the speech recognized event.
            recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs&gt;(recognizer_SpeechRecognized);

            // Configure the input to the speech recognizer.
            recognizer.SetInputToDefaultAudioDevice();
            // throws:
            // An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Speech.dll
            // Cannot find the requested data item, such as a data key or value.

            // Start asynchronous, continuous speech recognition.
            recognizer.RecognizeAsync(RecognizeMode.Multiple);

            // Keep the console window open.
            while (true)
            {
                Console.ReadLine();
            }
        }
    }
```

Microphone enabled:


Exception:

```'System.InvalidOperationException'
{"Cannot find the requested data item, such as a data key or value."}

   at Microsoft.Speech.Recognition.RecognizerBase.SetInputToDefaultAudioDevice()
   at Napoleon.Program.Main(String[] args) in C:\voice_recognition\Program.cs:line 42
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
```

Comment: Can you be more specific about the exception , a stack trace would be helpful as well.

Comment: Already asked http://stackoverflow.com/questions/32961817/microsoft-speech-recognition-setinputtodefaultaudiodevice-throws-exception and  http://stackoverflow.com/questions/33318596/microsoft-speech-speechrecognitionengine-setinputtodefaultaudiodevice-method-t

Comment: You need to use System.Speech API, not Microsoft.Speech API to work with a microphone.

Comment: updated the question. not much in it.

Comment: @NikolayShmyrev if you post it as an answer I will accept it.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:10/20/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><speech-recognition><microsoft-speech-api>', 'Title': "SetInputToDefaultAudioDevice throws 'System.InvalidOperationException' occurred in Microsoft.Speech.dll", 'ViewCount': 2413, 'id': 40165958, 'user_AboutMe': '', 'user_AccountId': 102880.0, 'user_DisplayName': 'Artur Kędzior', 'user_Reputation': 3864.0, 'user_Views': 534.0, 'user_id': 275882.0}}