using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Speech; using System.Speech.Synthesis; //using System.Speech.Recognition; using System.Text.RegularExpressions; namespace Say { class Say { static void Main(string[] args) { if (args.Length == 0) { System.Console.Out.Write( "Please provide text input for the computer to speak.\n" + "Input methods include passing Say.exe a filename,\n" + "or simply calling it like this:\n" + "\tSay.exe speak this text."); return; } SpeechSynthesizer ss = new SpeechSynthesizer(); int argLen = args.Length; // If the last thing looks like a WAV file, set that as the output file if(Regex.IsMatch(args[argLen - 1], @"\.wav", RegexOptions.IgnoreCase)) { ss.SetOutputToWaveFile(args[argLen - 1]); --argLen; // decrement argLen so that the filename isn't read aloud } // If the first thing is a file, read from it. if (File.Exists(args[0])) { string fileText = File.ReadAllText(args[0], Encoding.Default); ss.Speak(fileText); } else { StringBuilder sb = new StringBuilder(); for(int i = 0; i < argLen; ++i) { sb.Append(args[i] + " "); } string argString = sb.ToString() + "."; ss.Speak(argString); } } } }