UWP,对路径的访问被拒绝 [英] UWP, Access to the path is denied

查看:80
本文介绍了UWP,对路径的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些有关文件权限的主题.

I read some topic about file permission.

有人说应用程序可以访问用户使用FileOpenPicker或FolderPicker手动选择的目录和文件"

Someone said "App can access directories and files which the user manually selected with the FileOpenPicker or FolderPicker"

我的代码如下:

public async void CsvParse()
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.FileTypeFilter.Add(".csv");
    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        string[] lines = File.ReadAllLines(file.Path);//this is where app stops working and gives error message.
    }
}

即使我使用FilePicker选择文件,它仍然给我错误.但是,当我从appx文件夹中选择文件时,它可以正常工作.

Even when I choose file with FilePicker, it still gives me error. But when I choose file from appx folder, it works fine.

是否可以访问应用程序文件夹以外的其他位置?

Is there a way to access other locations than app's folder?

推荐答案

以这种方式尝试:

public async void CsvParse()
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.FileTypeFilter.Add(".csv");
            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                IList<string> lines = await FileIO.ReadLinesAsync(file);//this is where app stops working and gives error message.
            }
        }

StorageFile是访问文件的方式.File.ReadAllLines(file.Path)您传递的是文件名,而不是StorageFile,而仅文件路径不足以获取访问权限

the StorageFile is the way you get access to a file. File.ReadAllLines(file.Path) you are passing a Filename, not the StorageFile but just the filepath is not enough for getting access

这篇关于UWP,对路径的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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