【问题标题】:Windows FolderPicker stopped working after updating to Windows 10.0.18362Windows FolderPicker 在更新到 Windows 10.0.18362 后停止工作
【发布时间】:2019-09-09 21:20:25
【问题描述】:

我有使用 Windows FolderPicker 的代码。更新到 Windows 版本 10.0.18362 后,我对 FoldePicker 的使用已停止工作。

我附上了一些我用来获得访问文件被拒绝结果的代码。

using System;
using System.IO;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.Storage.AccessCache;

namespace FolderPickerTest
{
    public sealed partial class MainPage : Page
    {
        private static string path = @"filepath";
        string[] lines;

        public MainPage()
        {
            this.InitializeComponent();

            test();
        }

        public async void test()
        {
            var folderPicker = new FolderPicker();  
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;   
            folderPicker.FileTypeFilter.Add("*"); 

            StorageFolder folder = await folderPicker.PickSingleFolderAsync();  
            if (folder != null)
            {
                StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); 
            }
        }

        private void open_click(object sender, RoutedEventArgs e)
        {
            lines = new string[2];

            try
            {
                lines = File.ReadAllLines(path);
            }
            catch(Exception ex)
            {
                errorText.Text = ($"Error: {ex.Message.ToString()}");
            }
        }
    }
}

我目前收到的错误消息是: Access to the path 'filepath' is denied

【问题讨论】:

  • 你从哪里得到错误?
  • 您是否尝试过使用真实文件路径的代码?
  • 在UWP中,禁止直接通过路径访问文件。请使用FileOpenPicker 选择文件。你可以参考我的回答here,它做你想做的。

标签: c# windows uwp


【解决方案1】:

这里的问题是您正在尝试使用System.IO API 读取任意路径。在其中一个版本中,当您声明 broadFileSystemAccess 功能时,这实际上是有效的,但情况不再如此。您现在必须使用 StorageFile API 来实现您的目标。

如果您选择带有FolderPicker 的文件夹,您将得到一个StorageFolder 实例。您可以在此实例上调用GetFileAsync 方法以按名称获取文件。这是StorageFile 的一个实例,您可以使用FileIO.ReadLinesAsync method 读取它。

【讨论】:

    【解决方案2】:

    为了更好地解释 Martin Zikmund 的答案,在 Win 10 1803(2018 年 4 月)中,broadFileSystemAccess 功能在用户设置中自动设置为开启。

    从 Win 10 1809(2018 年 10 月)开始,此系统设置默认设置为关闭。

    您需要要求用户在“设置”应用中明确设置设置为 ON,即使直接引用特定设置页面也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-19
      • 2018-10-13
      • 1970-01-01
      • 2014-12-11
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多