AppendWildcard Method

GrammarBuilder.AppendWildcard Method

Appends a recognition grammar element that matches any input to the current sequence of grammar elements.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public void AppendWildcard()

The wildcard element is added to the end of the current sequence of elements.

The wildcard element matches any spoken word. It does not match background noise or silence.

The following example creates a grammar that accepts password input as a wildcard. The example attaches a Grammar.SpeechRecognized event handler to the grammar that validates the password input.

private Grammar CreatePasswordGrammar()
  GrammarBuilder wildcardBuilder = new GrammarBuilder();
  SemanticResultKey passwordKey =
    new SemanticResultKey("Password", wildcardBuilder);

  GrammarBuilder passwordBuilder =
    new GrammarBuilder("My Password is");

  Grammar passwordGrammar = new Grammar(passwordBuilder);
  passwordGrammar.Name = "Password input";

  passwordGrammar.SpeechRecognized +=
    new EventHandler<SpeechRecognizedEventArgs>(

  return passwordGrammar;

// Handle the SpeechRecognized event for the password grammar.
private void PasswordInputHandler(object sender, SpeechRecognizedEventArgs e)
  if (e.Result == null) return;

  RecognitionResult result = e.Result;
  SemanticValue semantics = e.Result.Semantics;

  if (semantics.ContainsKey("Password"))
    RecognizedAudio passwordAudio =
        result.Words[3], result.Words[result.Words.Count - 1]);

    if (IsValidPassword(passwordAudio))
      Console.WriteLine("Password accepted.");

      // Add code to handle a valid password here.
      Console.WriteLine("Invalid password.");

      // Add code to handle an invalid password here.

// Validate the password input. 
private bool IsValidPassword(RecognizedAudio passwordAudio)
  Console.WriteLine("Validating password.");

  // Add password validation code here.

  return false;

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft