0

I'm currently using .NET Framework 4.7.2 on Visual Studio 2019 to make a Win Form that converts audio formats. The problem is that I'm getting an Access Denied Error saying that I cant save my converted WAV file cannot be stored in a directory.

This is the part that gives me the error:

string rpath = AppDomain.CurrentDomain.BaseDirectory + "\\Converted";

        private void button3_Click(object sender, EventArgs e)
        {

            if (!Directory.Exists(rpath))
            {
                Directory.CreateDirectory(rpath);
            }
            else if (comboBox2.SelectedItem.ToString() == "WAV")
            {
                using (Mp3FileReader reader = new Mp3FileReader(textBox1.Text))
                {
                    WaveFileWriter.CreateWaveFile(rpath, reader);
                }
            }
        }

And this is the exact error:

System.UnauthorizedAccessException: 'Access to the path 'C:\Users\joswin\Documents\Converter\Converter\Converter\bin\Debug\Converted' is denied.'

My current guess is that NAudio's WaveFileWriter is causing the issue, but I don't know another way to convert audio files at the moment.

I've already tried running this in Administrator and yet it didn't work, it still gave me the same error.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 3
    It looks like you're trying to write the data to a Directory instead of a File. `CreateWaveFile()` probably wants a file path. You should also try to avoid to write to the executable path. When you deplyo, if the app is installed in `Program Files`, you won't probably have write rights there. You can write to a path that a User can select using a SaveFileDialog, or write to the `Temp` folder, then move the file to another folder (User input still required), or write to `Application.CommonAppDataPath` or `Application.LocalUserAppDataPath` or `Environment.SpecialFolder.MyDocuments` etc. – Jimi Mar 03 '21 at 14:56
  • Possibly, another instance of your program is still running and locked the file ? Or another program locked the file. You can change the file name to another one and try. – Subbu Mar 03 '21 at 15:12
  • @Jimi Thanks that worked for me – Joswin John Mar 03 '21 at 15:44
  • We don't put the answer in the question. If you have your own answer, post in in the answer box below. – LarsTech Mar 03 '21 at 15:52

0 Answers0