C# 在UWP应用程序中打开文件夹时拒绝访问
我有一个简单的UWP应用程序,我想打开一个文件夹来访问其中的所有文件。我正在使用微软的股票示例代码来说明如何在UWP中创建文件夹选择器。但是,在选择文件夹(任何文件夹)并尝试访问它之后,我总是会遇到以下异常:C# 在UWP应用程序中打开文件夹时拒绝访问,c#,uwp,C#,Uwp,我有一个简单的UWP应用程序,我想打开一个文件夹来访问其中的所有文件。我正在使用微软的股票示例代码来说明如何在UWP中创建文件夹选择器。但是,在选择文件夹(任何文件夹)并尝试访问它之后,我总是会遇到以下异常: System.UnauthorizedAccessException HResult=0x80070005 Message=Access to the path 'T:\temp' is denied. Source=System.IO.FileSystem StackTr
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
System.UnauthorizedAccessException
HResult=0x80070005
Message=对路径“T:\temp”的访问被拒绝。
Source=System.IO.FileSystem
堆栈跟踪:
位于System.IO.Enumeration.FileSystemumerator`1.CreateDirectoryHandle(字符串路径,布尔ignoreNotFound)
位于System.IO.Enumeration.FileSystemumerator`1..ctor(字符串目录,枚举选项)
位于System.IO.Enumeration.FileSystemEnumerable`1..ctor(字符串目录、FindTransform转换、枚举选项)
位于System.IO.Enumeration.FileSystemMemerableFactory.FileInfos(字符串目录、字符串表达式、枚举选项)
位于System.IO.DirectoryInfo.InternalEnumerateInfos(字符串路径、字符串搜索模式、搜索目标搜索目标、枚举选项)
位于System.IO.DirectoryInfo.EnumerateFiles()处
在C:\Users\nixca\source\repos\ShutterShock\ShutterShock\MainPage.xaml.cs中的ShutterShock.MainPage.d_u1.MoveNext()处:第37行
Mainpage.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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Threading.Tasks;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间百叶窗冲击
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
字符串路径=等待GetOpenLocation();
var boop=新目录信息(路径);
boop.EnumerateFiles();
}
异步任务GetOpenLocation()
{
字符串返回文本;
var folderPicker=new Windows.Storage.Pickers.folderPicker();
folderPicker.SuggestedStartLocation=Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(“*”);
Windows.Storage.StorageFolder folder=等待folderPicker.PickSingleFolderAsync();
如果(文件夹!=null)
{
//应用程序现在具有对所选文件夹中所有内容的读/写访问权限
//(包括其他子文件夹内容)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(文件夹);
returnText=folder.Path;
}
其他的
{
returnText=“操作已取消。”;//若要将此设置为异常,请捕获该异常
}
返回文本;
}
}
}
我在“boop.EnumerateFiles();”行中遇到了一个异常。因此,在发布此消息后,我当然会立即想到这一点,但我将保留此消息,因为在询问之前,我在任何地方都没有找到答案。微软的例子是愚蠢的,除非你想要的只是文件夹的路径,否则返回路径是没有用的。UWP filepicker实际上并没有授予您System.IO级别的文件夹访问权限,它只提供了一个可以使用的StorageFolder。如果您想做任何有用的事情,您需要返回StorageFolder对象,您可以使用它来实际处理文件。这对于真正知道自己在做什么的人来说可能是显而易见的。如果要获取选定文件夹中的所有文件,则可以使用
文件夹.getfileasync()
这是我的结果。
是的,让我感到困惑的只是整个过程中不得不使用StorageFolder而不是DirectoryInfo。谢谢你的例子。
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
var files = await folder.GetFilesAsync();
foreach(var file in files)
{
Debug.WriteLine(file.Path);
}
}