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

查看:27
本文介绍了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天全站免登陆