Reputation: 145
I'm trying to read a CSV file chosen with a OpenFilePicker
and put into the FutureAccessList
. But whenever I try to read it I get an DeniedAccessException
.
That function is a test :
private async Task readCSVCustomAsync()
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
StorageFile file;
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");
//picking a file with FilePicker
file = await openPicker.PickSingleFileAsync();
//Storing file in futureaccesslist
string faToken = StorageApplicationPermissions.FutureAccessList.Add(file);
//getting the file from FA list
var fileOpenTest = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(faToken);
//trying to read it
using (var reader = new StreamReader(fileOpenTest.Path)) //Exception here
using (var csv = new CsvReader(reader))
{
//elimination des premieres lignes avant le header
bool headerOK = false;
while (csv.Read() && !headerOK)
{
string rec = csv.GetField(0) + csv.GetField(1);
if (!rec.Equals(""))
{
csv.ReadHeader();
headerOK = true;
}
}
}
}
So here I'm trying to put the file in the FA List and retrieve it later to read the content (it's a csv file that I picked).
But even if I put it in the FutureAccessList
I get an AccessDeniedException
when I'm trying to read it, why I am getting that exception ?
Upvotes: 0
Views: 66
Reputation: 32785
Unable to read a file in FutureAccessList
The problem is you could not use System.IO.StreamReader
to access file where in the FutureAccessList
with path, and the path property only available in the Windows Storage API. So you need to open the file as steam, then invoke this var reader = new StreamReader(stream)
.
private async Task readCSVCustomAsync()
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
StorageFile file;
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");
//picking a file with FilePicker
file = await openPicker.PickSingleFileAsync();
//Storing file in futureaccesslist
string faToken = StorageApplicationPermissions.FutureAccessList.Add(file);
//getting the file from FA list
var fileOpenTest = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(faToken);
// open file as stream, to avoid using path property
var stream = await fileOpenTest.OpenStreamForReadAsync();
//trying to read it
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader))
{
//elimination des premieres lignes avant le header
bool headerOK = false;
while (csv.Read() && !headerOK)
{
string rec = csv.GetField(0) + csv.GetField(1);
if (!rec.Equals(""))
{
csv.ReadHeader();
headerOK = true;
}
}
}
}
Upvotes: 1