Guys, I need your help again in C#. I have a folder that contains txt files. I want to be able to convert the text to audio of any file in that folder. I know how to do the convestion but how to automatically convert whenever there is any new txt file in that folder. What I need is to be able to detect a new txt file and automate the conversion without the user opening the file manually.

Dani AI

Generated

As noted, FileSystemWatcher is the right starting point, and 's suggestion to create the output file programmatically is fine — but a few real-world patterns make this reliable for unattended conversion.

Never process the Created event immediately. Writers often create and then write to the file, so the file can be incomplete or locked. Two safe approaches: have the producer write a temp name and then rename to the final name (atomic signal), or have the consumer wait until the file is readable (retry/open or size-stability check). Use a queue (ConcurrentQueue or Task-based worker) so events are debounced and files are processed one by one. Example wait-for-file helper:

private bool WaitForFile(string path, int retries = 10, int delayMs = 200)
{
    while (retries-- > 0)
    {
        try
        {
            using (var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                return true;
            }
        }
        catch (IOException)
        {
            Thread.Sleep(delayMs);
        }
    }
    return false;
}

For automatic saving: choose a dedicated output folder (eg. "Converted") and build a safe name like the original base name plus a timestamp or GUID to avoid collisions. For text-to-speech, System.Speech.Synthesis.SpeechSynthesizer can write WAV; to produce MP3 you can encode with libraries such as NAudio or NAudio.Lame. See the SpeechSynthesizer docs (https://learn.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer) and NAudio on GitHub (https://github.com/naudio/NAudio) for encoder options.

Run the watcher/processor as a background service or scheduled task for true automation. Also increase FileSystemWatcher.InternalBufferSize if many changes occur, handle its Error event, log failures, and move problem files to an "errors" folder so they do not block processing. This combination removes the need for a SaveFileDialog and makes conversion robust.

Recommended Answers

All 3 Replies

Please take a look at FileSystemWatcher class. It should take care of your requirement.

Please take a look at FileSystemWatcher class. It should take care of your requirement.

Thanks, u have helped me.

Now my problem is: is there a way to save a file with a particular name without using the savefiledialog. U see I want to automate a save in my project. Whenever there is a new txt file in a folder being watched, i want to convert that file to audio and save the audio copy as mp3 automatically without the user doing the saving. Am able to convert the text to audio and save using savefiledialog but i want to automate it. Any help will be appreciated.

To automate it, you could create a new file at a specified location, then write the converted data to the new file.

if (!File.Exists(@"C:\convertedFile.mp3"))
{
	File.Create(@"C:\convertedFile.mp3");
}
else
{
	// File already exists, you have the option to delete this file and create the new converted file. (Uncomment next two lines)
	// File.Delete(@"C:\convertedFile.mp3");
	// File.Create(@"C:\convertedFile.mp3");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.