转换StreamReader(String)以使其与UWP API兼容? [英] Converting a StreamReader(String) to be compatible with UWP API?

查看:44
本文介绍了转换StreamReader(String)以使其与UWP API兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在利用StreamReader类遇到麻烦。在 StreamReader类文档页面上,该页面指出它
在版本信息标题通用Windows平台-从8开始可用下支持通用Windows平台(UWP)。

I'm running into a snag utilizing the StreamReader class. On the StreamReader Class documentation page, it states that it supports Universal Windows Platforms (UWPs) under the Version Information header, "Universal Windows Platform - Available since 8".

在进一步检查其构造函数后, StreamReader(Stream)构造函数确实支持UWP应用,但是 StreamReader(String)构造函数不支持它们。

Upon further inspection of its constructors, the StreamReader(Stream) constructors do support UWP apps, however the StreamReader(String) constructors do not support them.

我目前正在使用StreamReader(String)构造函数以及要读取的完整文件路径,

I'm currently using the StreamReader(String) constructor with the complete file path to to be read,

using (StreamReader sr = new StreamReader(path))
{
    ...
}

我正在寻求学习如何将我的代码从StreamReader(String)转换为StreamReader(Stream)。

I'm seeking to learn how to convert my code for a StreamReader(String) to a StreamReader(Stream).

推荐答案

在UWP中 StreamReader 仅接受 Stream 和其他选项。不是字符串。

In UWP StreamReader accepts only Stream with additional Options. Not String.

因此要从特定路径使用 StreamReader ,您需要获取 StorageFile

So to use StreamReader from a particular path, you need to get the StorageFile

StorageFile file = await StorageFile.GetFileFromPathAsync(<Your path>);
var randomAccessStream = await file.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();
StreamReader str = new StreamReader(stream);

这篇关于转换StreamReader(String)以使其与UWP API兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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