Tuesday 28 May 2024

ASP.NET Hosting Tutorial: How to Create Dynamic Narration in Scripts with System.Speech and .NET 8?

Leave a Comment
I'll provide you with the code to incorporate voice interactions into your Microsoft Windows scripts in this article.


This page has the source code; download it and proceed as instructed.
The downloaded file should be unzipped.
Open the solution in Visual Studio.
Check the code on the Program. cs and check the code.
    using System.Speech.Synthesis;
    
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            PromptSpeech(args.Length == 0 ? ["Please pass the command to speech!"] : args);
        }
    
        private static void PromptSpeech(string[] args)
        {
            var voice = new SpeechSynthesizer();
            voice.SetOutputToDefaultAudioDevice();
            voice.Rate = 1;
            voice.Speak(string.Join(" ", args));
        }
    }
On the Solution Explorer, right-click on SpeechPrompt and select Publish.
http://hostforlife.eu

Publish option.

Click the Publish button.


After the publish, click on the icon to copy the path to the clipboard.

Copy the .exe file.

to C:\SpeechCmd.

How do I use this command?

Call from your bat file.

C:\SpeechCmd\SpeechPrompt.exe WITH YOUR MESSAGE TO SPEECH

or from PowerShell.

Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "MESSAGE TO SPEECH"

Now you can try this batch script sample.bat attached to the project.

@echo off
C:\SpeechCmd\SpeechPrompt.exe Please confirm the drivers backup pressing enter
echo Please confirm the drivers backup pressing enter
pause
REM Check if the temporary directory exists and create it if it doesn't
if not exist C:\temp\driversWindow (
    C:\SpeechCmd\SpeechPrompt.exe Creating temporary directory
    mkdir C:\temp\driversWindow
)


C:\SpeechCmd\SpeechPrompt.exe  Copying files from the drivers directory to the temporary directory
REM Copy files from the drivers directory to the temporary directory
xcopy C:\Windows\System32\drivers\* C:\temp\driversWindow\ /E /H /C /I

REM Change to the temporary directory
cd /d C:\temp

REM Compress the files into a ZIP archive using PowerShell
C:\SpeechCmd\SpeechPrompt.exe Compressing files into a ZIP archive
powershell Compress-Archive -Path C:\temp\driversWindow\* -DestinationPath C:\temp\driversWindow.zip

REM Check if the ZIP file was created successfully
if exist C:\temp\driversWindow.zip (
    REM Delete the temporary directory and its contents
    rd /s /q C:\temp\driversWindow
) else (
    C:\SpeechCmd\SpeechPrompt.exe Zip file creation failed. Temporary files not deleted.
    echo Zip file creation failed. Temporary files not deleted.
)

REM Notify that the process is complete

echo Process complete. The ZIP file is located at C:\temp\driversWindow.zip
C:\SpeechCmd\SpeechPrompt.exe Process complete. The ZIP file is located at C:\temp\driversWindow.zip
C:\SpeechCmd\SpeechPrompt.exe Thanks for using this script
pause

If you start the sample.bat, it will complete smoothly, but the sample.ps1 in PowerShell will not complete successfully because PowerShell can't access the System32\Drivers subfolders.

And try the sample.ps1.

# PowerShell script equivalent of the provided batch script

# Notify the user to confirm the drivers backup
Write-Host "Please confirm the drivers backup pressing enter"
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Please confirm the drivers backup pressing enter"

Read-Host -Prompt "Press Enter to continue..."

# Check if the temporary directory exists and create it if it doesn't
$temporaryDir = "C:\temp\driversWindow"
if (-Not (Test-Path $temporaryDir)) {
    Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Creating temporary directory"
    New-Item -ItemType Directory -Path $temporaryDir
}

# Notify the user that files are being copied
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Copying files from the drivers directory to the temporary directory"

# Copy files from the drivers directory to the temporary directory
$sourceDir = "C:\Windows\System32\drivers\*"
Copy-Item -Path $sourceDir -Destination $temporaryDir -Recurse -Force

# Notify the user that files are being compressed
Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Compressing files into a ZIP archive"

# Compress the files into a ZIP archive
$zipFilePath = "C:\temp\driversWindow.zip"
Compress-Archive -Path "$temporaryDir\*" -DestinationPath $zipFilePath -Force

# Check if the ZIP file was created successfully
if (Test-Path $zipFilePath) {
    # Delete the temporary directory and its contents
    Remove-Item -Path $temporaryDir -Recurse -Force
    # Notify the user that the process is complete
    $completionMessage = "Process complete. The ZIP file is located at $zipFilePath"
    Write-Host $completionMessage
    Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList $completionMessage
    Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Thanks for using this script"

} else {
    # Notify the user of the failure
    Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Zip file creation failed. Temporary files not deleted."
    Write-Host "Zip file creation failed. Temporary files not deleted."
     Start-Process -NoNewWindow -Wait "C:\SpeechCmd\SpeechPrompt.exe" -ArgumentList "Sorry for the inconvenience"
}


Read-Host -Prompt "Press Enter to exit..."

Best ASP.NET Core 9.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment