[英]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:
您仍然可以通过以下两种方法来完成任务:
var path = @"WriteLines.xls"
var folder = KnownFolders.DocumentsLibrary;
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.