我对这个看似简单的任务的明显复杂性感到非常震惊。我知道我必须使用 StorageFile
类,我找到了这个 example ,但我只想读取一个我知道路径的文件,并将其数据作为文本读取到字符串中。
根据我收集到的信息,使用 StorageFile
读取文件,我必须经过一堆接口(interface); IAsyncOperation<StorageFile>
和 IAsyncOperationCompletedHandler
.
一定有更好(更简单)的方法。像这样的东西:
using (StorageFile sf = StorageFile.OpenAsync("myFile.txt"))
{
string line = sf.ReadLine();
}
显然这行不通,但也许我漏掉了什么,或者有人可以向我解释如何以不同的方式读取文件?
最佳答案
此网页可能会有帮助:http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
相关代码:
public string CurrentFileBuffer
{
get; private set;
}
public async void ReadTextFile(string Path)
{
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(Path);
var read = await FileIO.ReadTextAsync(file);
CurrentFileBuffer = read;
}
关于c# - 如何读取文件 (Metro/WinRT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12703816/