无法读取FutureAccessList中的文件

时间:2019-05-02 09:51:56

标签: c# uwp

我正在尝试读取用OpenFilePicker选择的CSV文件并将其放入FutureAccessList。但是,每当我尝试阅读它时,我都会得到DeniedAccessException

该功能是一个测试:

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;
            }
        }
    }
}

所以在这里,我试图将文件放入FA列表中,并在以后检索它以读取内容(这是我选择的一个csv文件)。

但是,即使我将其放在FutureAccessList中,但在尝试阅读时还是得到了AccessDeniedException,为什么我会收到该异常?

1 个答案:

答案 0 :(得分:1)

  

无法读取FutureAccessList中的文件

问题是您无法使用System.IO.StreamReader访问FutureAccessList中带有路径的文件,而path属性仅在Windows Storage API中可用。因此,您需要以流方式打开文件,然后调用此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;
            }
        }
    }
}