0

I'm creating an UWP application to split CSV and Excel files. The user has to select a file in any folder on the machine and the application has to read the file. At the end of the process, the application has to save one or more files based on what the user wants.

When I try to read a CSV file with this code

using (var reader = new StreamReader(file))
using (var csv = new CsvReader(reader, config))
{
    return csv.GetRecords<dynamic>();
}

I receive an error

System.UnauthorizedAccessException: 'Access to the path 'C:\Users\enric\Downloads\combined_data.csv' is denied.'

enter image description here

I checked the capabilities in the Package.appxmanifest but there is none related to files or folders. There is only Removable Storage that seems similar to what I'm looking for but it doesn't work.

enter image description here

In the Microsoft documentation, I read to add a custom capability

<Capabilities><uap:Capability Name="documentsLibrary"/></Capabilities>

but it doesn't work either.

Enrico
  • 3,592
  • 6
  • 45
  • 102
  • https://stackoverflow.com/questions/4877741/access-to-the-path-is-denied This link may be helpful – Jack Arnold May 06 '21 at 16:13
  • Thank you @JackArnold but that post is related to IIS not UWP – Enrico May 06 '21 at 16:16
  • 1
    https://stackoverflow.com/questions/60945529/uwp-how-to-get-access-to-file-in-folder is relevant - you don't have access to the path directly, but you can get a stream from the StorageFile returned from the FileOpenPicker. If the user's explicitly picking files you don't need any special capabilities. – Rob Caplan - MSFT May 06 '21 at 17:45
  • Does this answer your question? [UWP how to get access to file in folder](https://stackoverflow.com/questions/60945529/uwp-how-to-get-access-to-file-in-folder) – Rob Caplan - MSFT May 06 '21 at 17:45

1 Answers1

0

You don't need to specify capabilities, just use pick file or read file code, this code can be also found in Microsoft doc and doc.

private async void Btn_OpenFile_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            try
            {
                FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add(".csv");
                picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

                var file = await picker.PickSingleFileAsync();
                if (file != null)
                {
                    
                }
            }
            catch
            {
                
            }
        }
Vincent
  • 3,124
  • 3
  • 21
  • 40
  • This is if I want to use the file when I use the `FileOpenPicker`. I want to open the file later or in a procedure in background. – Enrico May 07 '21 at 07:35
  • Maybe you can try `FutureAccessList`, see https://stackoverflow.com/a/38107910/7068790 – Vincent May 07 '21 at 12:50