简体   繁体   English

来自绝对文件路径的UWP中的ReadLinesAsync

[英]ReadLinesAsync in UWP from absolute file path

I get error 我得到错误

The filename, directory name, or volume label syntax is incorrect. 文件名,目录名称或卷标签语法不正确。 (Exception from HRESULT: 0x8007007B) (来自HRESULT的异常:0x8007007B)

My code is 我的代码是

public async void ReadFile()
    {     
        var path = @"F:\VS\WriteLines.xls";
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        var file = await folder.GetFileAsync(path);
        var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
        foreach (var line in readFile.OrderBy(line =>
        {
            int lineNo;
            var success = int.TryParse(line.Split(';')[4], out lineNo);
            if (success) return lineNo;
            return int.MaxValue;
        }))
        {
            itemsControl.Items.Add(line);
        }
    }

The error shows up at var file = await folder.GetFileAsync(path); 该错误显示在var file = await folder.GetFileAsync(path);

You cannot read a file from an arbitrary location on disk in a UWP App. 您无法从UWP App中磁盘上的任意位置读取文件。 There are a couple of ways you can still accomplish your task: 您仍然可以通过以下两种方法来完成任务:

  1. You can add the WriteLines.xls file to your project and set it's build action to Content and Copy to output Directory to Copy if newer . 您可以将WriteLines.xls文件添加到项目中,并将其生成操作设置为Content,然后将其复制到输出目录如果较新则复制Copy You can then access the file with the code you have by simply replacing the "path" value to: 然后,您可以通过简单地将“ path”值替换为以下内容来使用具有的代码访问文件:

var path = @"WriteLines.xls"

More details here 在这里更多细节

  1. If you need to be able to read any files from disk, you need to either use a FilePicker to select the file or copy the file in the Documents Folder and change the folder to: 如果您需要能够从磁盘读取任何文件,则需要使用FilePicker选择文件或将文件复制到“文档文件夹”中,并将文件夹更改为:

var folder = KnownFolders.DocumentsLibrary;

More details here 在这里更多细节

You are asking for file with absolute path from application's local folder - hence it throws that error as you provide path that includes drive name. 您要从应用程序的本地文件夹中获取具有绝对路径的文件-因此,当您提供包含驱动器名称的路径时,它将引发该错误。

In general UWP is very restrictive on where/how you can get files from - I don't think you can get it from absolute path in the sample (app needs more permissions to get to similar places). 通常,UWP对于从何处/如何获取文件有严格的限制-我认为您不能从示例中的绝对路径获取文件(应用需要更多权限才能访问类似位置)。 You can try StorageFile.GetFileFromPathAsync . 您可以尝试StorageFile.GetFileFromPathAsync

Detailed info on locations app can access - UWP apps on Windows 10: File access permissions . 有关位置应用程序的详细信息可以访问Windows 10上的-UWP应用程序:文件访问权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM