Home > Enterprise >  Folder acess denied wpf
Folder acess denied wpf

Time:08-27

I am opening a FileDialog, to let the user select an Audio file, in an MP3 file, and convert it to WAV, the error appears when I am trying to save the file in a new folder that I am creating

var dlg = new OpenFileDialog
            {
                DefaultExt = ".mp3",
                Filter = "Audio files (.mp3)|*.mp3"
            };

            var res = dlg.ShowDialog();

            if (res! == true)
            {
                var projectPath = Directory.GetParent(Directory.GetCurrentDirectory())?.Parent?.Parent?.FullName;
                var FoderName = Path.Combine(projectPath!, "Audios");
                Directory.CreateDirectory(FoderName);

                using (var mp3 = new Mp3FileReader(dlg.FileName))
                {
                    using (var ws = WaveFormatConversionStream.CreatePcmStream(mp3))
                    {
                        WaveFileWriter.CreateWaveFile(FoderName, was) // Error 
                        System.UnauthorizedAccessException: 'Access to the path 
                      'C:\XXX\XXX\XXX\XXX\XXX\XXX' is denied.'

                    }

                }

Thanks

CodePudding user response:

Inside some folders including "Program Files", an app usually has no permission to write a file. Therefore, it is recommended to use a folder dedicated for a specific purpose and made available for apps (you can get path to "Music" by Environment.GetFolderPath(Environment.SpecialFolder.MyMusic).

In general, an app must expect UnauthorizedAccessException when attempting to write a file and prepare a fallback for the case of that exception.

  •  Tags:  
  • wpf
  • Related