首页 > 解决方案 > 在 UWP 应用中打开文件夹时访问被拒绝

问题描述

我有一个简单的 UWP 应用程序,我想打开一个文件夹来访问其中的所有文件。我正在使用 Microsoft 的股票示例代码来了解如何在 UWP 中制作文件夹选择器。但是,在选择文件夹(任何文件夹)并尝试访问它之后,我总是得到这个异常:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'T:\temp' is denied.
  Source=System.IO.FileSystem
  StackTrace:
   at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
   at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, EnumerationOptions options)
   at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options)
   at System.IO.Enumeration.FileSystemEnumerableFactory.FileInfos(String directory, String expression, EnumerationOptions options)
   at System.IO.DirectoryInfo.InternalEnumerateInfos(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
   at System.IO.DirectoryInfo.EnumerateFiles()
   at ShutterShock.MainPage.<Button_Click>d__1.MoveNext() in C:\Users\nixca\source\repos\ShutterShock\ShutterShock\MainPage.xaml.cs:line 37

主页.xaml.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace ShutterShock
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
             string path = await GetOpenLocation();

            var boop = new DirectoryInfo(path);

            boop.EnumerateFiles();

        }

        async Task<string> GetOpenLocation()
        {

            string returnText;

            var folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add("*");

            Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();

            if (folder != null)
            {
                // Application now has read/write access to all contents in the picked folder
                // (including other sub-folder contents)
                Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);
                returnText = folder.Path;
            }
            else
            {
                returnText = "Operation cancelled."; //todo make this an exception, catch that exception
            }

            return returnText;
        }

    }
}

我在“boop.EnumerateFiles();”上得到了异常 线。

标签: c#uwp

解决方案


所以当然在发布这个之后它就来了,但我会留下这个,因为在询问之前我实际上并没有在任何地方找到答案。微软的例子是愚蠢的,除非你想要的只是文件夹的路径,否则返回路径是没有用的。UWP 文件选择器实际上并没有授予您对文件夹的 System.IO 级别访问权限,它为您提供的只是您可以使用的 StorageFolder。如果您想做任何有用的事情,您需要返回 StorageFolder 对象,您可以使用它来实际处理文件。这对于真正知道自己在做什么的人来说可能是显而易见的。


推荐阅读