.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. namespace SytemSpeechNETCore3
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. Speak("Hello World!");
  12. }
  13. private static void Speak(string textToSpeech, bool wait = false)
  14. {
  15. // Command to execute PS
  16. Execute($@"Add-Type -AssemblyName System.speech;
  17. $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;
  18. $speak.Speak(""{textToSpeech}"");"); // Embedd text
  19. void Execute(string command)
  20. {
  21. // create a temp file with .ps1 extension
  22. var cFile = System.IO.Path.GetTempPath() + Guid.NewGuid() + ".ps1";
  23. //Write the .ps1
  24. using var tw = new System.IO.StreamWriter(cFile, false, Encoding.UTF8);
  25. tw.Write(command);
  26. // Setup the PS
  27. var start =
  28. new System.Diagnostics.ProcessStartInfo()
  29. {
  30. FileName = "C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe", // CHUPA MICROSOFT 02-10-2019 23:45
  31. LoadUserProfile = false,
  32. UseShellExecute = false,
  33. CreateNoWindow = true,
  34. Arguments = $"-executionpolicy bypass -File {cFile}",
  35. WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
  36. };
  37. //Init the Process
  38. var p = System.Diagnostics.Process.Start(start);
  39. // The wait may not work! :(
  40. if (wait) p.WaitForExit();
  41. }
  42. }
  43. }
  44. }
CONCLUSION


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

Happy coding.