当我对RichEditBox UWP C#的打开文本文件使用打开文件选择器时,访问被拒绝 [英] Acces Denied when I use Open File Picker for open text file for RichEditBox UWP C#

查看:186
本文介绍了当我对RichEditBox UWP C#的打开文本文件使用打开文件选择器时,访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用打开文件选择器"打开文本文件并在RichEditBox中显示,但是当我选择文件并按确定"时,Visual Studio显示"Access Denied",我想知道如何解决这个问题,我的代码:

I want to open a text file with Open File Picker and show in a RichEditBox, but when I select the file and push Ok, Visual Studio show "Access Denied", I want to know how to solve this please, there is my code:

var picker = new FileOpenPicker();
        picker.ViewMode = PickerViewMode.Thumbnail;
        picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        picker.FileTypeFilter.Add("*");
        picker.FileTypeFilter.Add(".txt");
        picker.FileTypeFilter.Add(".text");
        picker.FileTypeFilter.Add(".bat");
        picker.FileTypeFilter.Add(".js");
        picker.FileTypeFilter.Add(".vbs");

        StorageFile file = await picker.PickSingleFileAsync();
        if (file != null)
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile filepath = await StorageFile.GetFileFromPathAsync(file.Path);
            string text = await FileIO.ReadTextAsync(filepath);
            RichEditBox1.Document.SetText(Windows.UI.Text.TextSetOptions.None, text);

        }

推荐答案

您不需要调用StorageFile.GetFileFromPathAsync(file.Path),因为在PickSingleFileAsync返回的file变量中已经具有此StorageFile:

You don't need to call StorageFile.GetFileFromPathAsync(file.Path) since you already have this StorageFile in the file variable returned from PickSingleFileAsync:

    StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        string text = await FileIO.ReadTextAsync(file);
        RichEditBox1.Document.SetText(Windows.UI.Text.TextSetOptions.None, text);
    }

不必要的GetFileFromPathAsync可能会引发AccessDenied错误,因为FileOpenPicker仅通过返回的StorageFile提供访问,而不能通过其路径直接访问文件.此行为取决于版本,并且Windows 10的新版本将允许通过文件系统API进行更直接的访问(请参阅Build 2017谈话 UWP Apps文件访问权限改进

The unnecessary GetFileFromPathAsync probably throws an AccessDenied error since the FileOpenPicker provides access only through the returned StorageFile and doesn't give direct access to the file through its path. This behavior is version dependent and new versions of Windows 10 will allow more direct access through file system API (see the Build 2017 talk UWP Apps file access improvements

这篇关于当我对RichEditBox UWP C#的打开文本文件使用打开文件选择器时,访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆