Best Cloud ASP.NET Hosting

If you are looking for the best ASP.NET hosting, you are in the right place. Here we discuss all about web hosting. How to find the right web hosting, best web hosting recommendations, and articles, tips and tricks about web hosting.

Using System.Speech with NET Core 3.0

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:
using System;  
using System.Text;  
using System.Windows.Forms;  
namespace SytemSpeechNETCore3  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            Speak("Hello World!");  
        }  
        private static void Speak(string textToSpeech, bool wait = false)  
        {  
            // Command to execute PS  
            Execute([email protected]"Add-Type -AssemblyName System.speech;  
            $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;                           
            $speak.Speak(""{textToSpeech}"");"); // Embedd text  
            void Execute(string command)  
            {  
                // create a temp file with .ps1 extension  
                var cFile = System.IO.Path.GetTempPath() + Guid.NewGuid() + ".ps1";  
                //Write the .ps1  
                using var tw = new System.IO.StreamWriter(cFile, false, Encoding.UTF8);  
                tw.Write(command);  
                // Setup the PS  
                var start =  
                    new System.Diagnostics.ProcessStartInfo()  
                    {  
                        FileName = "C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe",  // CHUPA MICROSOFT 02-10-2019 23:45                    
                        LoadUserProfile = false,  
                        UseShellExecute = false,  
                        CreateNoWindow = true,  
                        Arguments = $"-executionpolicy bypass -File {cFile}",  
                        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden  
                    };  
                //Init the Process  
                var p = System.Diagnostics.Process.Start(start);  
                // The wait may not work! :(  
                if (wait) p.WaitForExit();  
            }  
        }  
    }  
}
CONCLUSION
With this code, your Windows based App can speech. It works on Windows 7, 8.1 and 10.
Rajani Prakash

    error: Content is protected !!