如何解决 UWP 应用程序读取文件时权限被拒绝的问题? [英] How to solve the problem of Permission denied while reading a file from UWP application?

查看:58
本文介绍了如何解决 UWP 应用程序读取文件时权限被拒绝的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UWP 应用程序中读取 C 或 D 驱动器的 .txt 文件.没关系,当我在资产中声明文件名的局部变量时.但它也用于从其他目录或驱动器读取文件.

I am trying to reading a .txt file of C or D drive in a UWP application. It is ok, when I declare the local variable for file name in assets. But it's filed to read file from others directory or drives as well.

我尝试过如下代码部分:

I have tried like the code portion below:

try
{
    String FileName = "C:\Users\abc\Documents\file.txt";
    // Read the file and display it line by line.  
    string[] lines = File.ReadAllLines(FileName);
    foreach (string inputLine in lines)
    {
        List<String> dummyList = new List<String>();
        String[] lineList = inputLine.Split(", ");

        foreach (var item in lineList)
        {
            dummyList.Add(item);
            //Debug.WriteLine(item);
        }
        mainList.Add(dummyList);
    }

    //file.Close();
}
catch (Exception ex)
{
    Debug.WriteLine("[DEBUG] " + ex);
}

file.txt"对所有用户和应用程序都具有本地读写权限.它是通过 windows 中的 file.txt 属性完成的.

The "file.txt" has both read and write permission locally for all users and applications. It has done from the file.txt properties in windows.

异常如下:

System.UnauthorizedAccessException: Access to the path 'C:\Users\abc\Documents\file.txt' is denied.
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
   at Localizer.Data.StringDataText.StringDataInitialize()

推荐答案

正如 Adam McMahon 所说,UWP 应用是沙盒应用,不直接访问外部环境中的文件.对于文件权限,您可以查看此文档.

As Adam McMahon said, UWP apps are sandboxed apps and don't directly access files in the external environment. For file permissions, you can view this document.

针对您提供的示例,您可以使用 FileOpenPicker 选择一个文本文件并从获取的 StorageFile 中读取内容.

Specific to the example you provided, you can use FileOpenPicker to select a text file and read the contents from the obtained StorageFile.

此示例仅供参考:

public async static Task<StorageFile> OpenLocalFile(params string[] types)
{
    var picker = new FileOpenPicker();
    picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    Regex typeReg = new Regex(@"^\.[a-zA-Z0-9]+$");
    foreach (var type in types)
    {
        if (type == "*" || typeReg.IsMatch(type))
            picker.FileTypeFilter.Add(type);
        else
            throw new InvalidCastException("Invalid extension");
    }
    var file = await picker.PickSingleFileAsync();
    if (file != null)
        return file;
    else
        return null;
}

使用

var txtFile=await OpenLocalFile(".txt");
if(txtFile!=null)
{
    var lines = FileIO.ReadLinesAsync(txtFile);
    // ... Other code
}

最好的问候.

这篇关于如何解决 UWP 应用程序读取文件时权限被拒绝的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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