c# - 如何读取文件 (Metro/WinRT)

标签 c# wpf windows-8 microsoft-metro windows-runtime

我对这个看似简单的任务的明显复杂性感到非常震惊。我知道我必须使用 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/

相关文章:

c# - 无法加载文件或程序集 System.Threading.Tasks,版本 = 2.5.19.0

c# - UDP 广播不能在 Windows 8 中的同一台机器上工作

c# - .NETCore(Windows 8 框架)的 `GetCustomAttributes` 的等效方法是什么?

c# - 在网格中使用 2 个不同的 ViewModel

c++ - 如何在 Windows 8 现代应用程序上从视频流中抓取帧?

c# - Sql 批量复制是如何工作的?

c# - 添加方法后有关静态局部函数的错误

c# - NewRelic 忽略单个页面的监控

c# - 如何在不覆盖现有样式的情况下向 WPF 自定义控件添加触发器?

c# - Datagrid 中的 NumericUpDown