Search code examples
c#speech-synthesis

How to give a text file as an input to speech synthesis Speak method


I am developing an application in C# where I would like to read from a simple text file. So how to give input(the file) to speak synthesis Engine.Speak() method.


Solution

  • According to your comment and this guide

    I'll give you example how to get a text file's content into your program.

    Example 1: Read all text and save into string.

     string alltext= System.IO.File.ReadAllText(@"read.txt");
     Engine.Speak(alltext); //put the string into your command
    

    Example 2: Read each line of file and save into string array.

     string[] eachlinetext = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
     Engine.Speak(eachlinetext[0]); // speak the first line of the file
    

    Hope it helps you. Also , Happy Programming!!