Using System.Speech with NET Core 3.0

.NET Core 3.0 is the latest version of .NET Core and now supports WinForms and WPF. One of the functionality that just got imported from .NET Framework to .NET Core is speech. Now, we can build speech enabled apps using .NET Core 3.0.
 

About Sytem.Speech

 
System.Speech is a native .NET Framework 4.x, 3.x assembly that implements speech related functionality including text to speech and speech to text. This post is not about System.Speech, but how to use it with .NET Core 3.0. You can download the code of this blog and start using it. More information about System.Speech you can get at Microsoft Documentation.
 
How it is implemented
  
You cannot call directly NET Framework from NET Core.
 
How it works
 
See code bellow: 
  1. using System;  
  2. using System.Text;  
  3. using System.Windows.Forms;  
  4.   
  5. namespace SytemSpeechNETCore3  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.             Speak("Hello World!");  
  13.         }  
  14.         private static void Speak(string textToSpeech, bool wait = false)  
  15.         {  
  16.             // Command to execute PS  
  17.             Execute($@"Add-Type -AssemblyName System.speech;  
  18.             $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;                           
  19.             $speak.Speak(""{textToSpeech}"");"); // Embedd text  
  20.   
  21.             void Execute(string command)  
  22.             {  
  23.                 // create a temp file with .ps1 extension  
  24.                 var cFile = System.IO.Path.GetTempPath() + Guid.NewGuid() + ".ps1";  
  25.   
  26.                 //Write the .ps1  
  27.                 using var tw = new System.IO.StreamWriter(cFile, false, Encoding.UTF8);  
  28.                 tw.Write(command);  
  29.   
  30.                 // Setup the PS  
  31.                 var start =  
  32.                     new System.Diagnostics.ProcessStartInfo()  
  33.                     {  
  34.                         FileName = "C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe",  // CHUPA MICROSOFT 02-10-2019 23:45                    
  35.                         LoadUserProfile = false,  
  36.                         UseShellExecute = false,  
  37.                         CreateNoWindow = true,  
  38.                         Arguments = $"-executionpolicy bypass -File {cFile}",  
  39.                         WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden  
  40.                     };  
  41.   
  42.                 //Init the Process  
  43.                 var p = System.Diagnostics.Process.Start(start);  
  44.                 // The wait may not work! :(  
  45.                 if (wait) p.WaitForExit();  
  46.             }  
  47.         }  
  48.   
  49.     }  
  50. }  
CONCLUSION

 
With this code, your Windows based App can speech. It works on Windows 7, 8.1 and 10.
 
Happy coding.