2
    private void ReadText(string text)
    {
        string add = "http://translate.google.com/translate_tts?tl=ta&q=";
        add += HttpUtility.UrlEncode(text, Encoding.GetEncoding("utf-8"));
        try
        {
            using (var client = new WebClient())
            {
                try
                {
                    client.Headers[HttpRequestHeader.UserAgent] =
                        "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";                                            }
                catch
                {
                    MessageBox.Show("Intenet error");
                }

                try
                {                        
                    client.DownloadFile(add, "mp3CriationTest.mp3");
                }
                catch 
                {
                    MessageBox.Show("NAudio Error");
                }
            }
        }
        catch (WebException e3)
        {
            MessageBox.Show("ReadText error: " + e3);

        }
    }

I am using nAudio to do google text-to-speech(tts). This code working much better for me when I run on visual studio debug(F5). But When I created setup file I ll get the exception message "NAudio Error". So its obvious there is a problem on saving/creating "mp3CriationTest.mp3" on Application folder of my setup. But Creating "mp3CriationTest.mp3" on debug folder is works fine when I use Visual Studio debug(F5). Can anyone know what was the issue? Plaese help.

  • 1
    You should have a look at the stacktrace of your caught exception, instead of swallowing it and trick yourself by just showing up a messagebox which literally tells you nothing. ;-) – Fabian Bigler Jul 26 '13 at 15:37
  • Also, it's bad practise to overuse try catch: http://stackoverflow.com/questions/1722964/when-to-use-try-catch-blocks – Fabian Bigler Jul 26 '13 at 15:39

1 Answers1

3

You cannot write directly into the program files folder unless you have administrative privileges. You can save the file to the application data folder (environment specified); like

string fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + "mp3test.mp3";
client.DownloadFile(add, fileName);
Leon
  • 919
  • 6
  • 21
  • 3
    Better to use `Path.Combine(AppDataFolder, "mp3File")`, even better to create an application folder of your own there :) – Sayse Jul 26 '13 at 15:44
  • agree with the creation of a new folder – Leon Jul 26 '13 at 15:46
  • @Leon can you please tell me how to create "mp3CriationTest.mp3" file into the application folder rather than retrieving it. Let's say if I copy "mp3CriationTest.mp3" manually into that application folder then program works fine. What I want is to create new "mp3CriationTest.mp3" into Application folder without conflicting "client.DownloadFile(add, "mp3CriationTest.mp3");" method. – Sutharshan Suthan Jul 26 '13 at 17:13
  • Found the issue. I want the writing permission to my application folder. – Sutharshan Suthan Jul 26 '13 at 18:06
  • If I'm not mistaking, you should (as each program should) have access to that folder.. The file should be created (with the sample) in that folder without any problems. If any problems should occur - add an exception value to the "Naudio error" catch and post that error in an update / edit – Leon Jul 26 '13 at 18:07