c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

The process cannot access the file Directory because it is being used by another process.

| | ASP-NET , CSharp

I got the following error while I try to create a file using c# .Net. It is happening because that file is being used by another process only one to access the file.

You should close the connection when you are using an object. The using statement simplifies the code that you have to write to create and clean the object.

I got error for the following code,

protected void btnSpeak_Click(object sender, EventArgs e)
    {
        SpeechAudioFormatInfo info = new SpeechAudioFormatInfo(6, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
        SpeechSynthesizer ss = new SpeechSynthesizer();
       ss.Volume = 100;
       ss.SelectVoiceByHints(VoiceGender.Female,VoiceAge.Adult);
       ss.SetOutputToWaveFile(@"D:\MyAudioFile.wav");
       ss.Speak(txtContent.Text);
    }

Exception message:

Solution:

You can write code like this,

using (var ss = new SpeechSynthesizer())
        {
           ss.Volume = 100;
           ss.SelectVoiceByHints(VoiceGender.Female,VoiceAge.Adult);
           ss.SetOutputToWaveFile(
@"D:\MyAudioFile.wav");
           ss.Speak(txtContent.Text);
           
}