【问题标题】:Can not create a StreamReader class from a string无法从字符串创建 StreamReader 类
【发布时间】:2016-03-24 22:18:38
【问题描述】:

我无法从字符串创建 StreamReader 对象。代码如下:

 StreamReader reader = new StreamReader(filePath);

这是我得到的确切错误:

'StreamReader'(StreamReader(Stream)) 的最佳重载匹配有一些无效参数

这没有任何意义,我查看了 microsoft 网站和其他任何地方(包括本网站)上的文件 IO,只是没有找到 StreamReader 的字符串构造函数。我使用 VS 2013 专业版作为我的 IDE,并且我也在使用 monogame(XAML) 模板。

【问题讨论】:

  • 假设你引用 System.IO;那 filePath 确实是一个字符串?你能包含更多代码吗?

标签: c# streamreader


【解决方案1】:

There is a StreamReader(string) constructor,但不是在所有配置文件中 - 请注意 PCL 和 Windows 应用商店应用程序的过载是 missing the icons。所以要么;

  1. 构造函数重载受限于(不存在于)目标配置文件,或者

  2. 范围内有不同的 StreamReader。

鉴于错误消息表明它正在尝试与 StreamReader(Stream) 进行最佳匹配,可能的原因是 #1,因为在 Windows 应用商店应用程序中可用的流式构造函数。 p>

【讨论】:

    【解决方案2】:

    好的,我想通了。显然您可以使用TitleContainer.OpenStream(String) 创建一个流。

    这是代码行。

    StreamReader reader = new StreamReader(TitleContainer.OpenStream(filePath));
    

    【讨论】:

    • 当然。这会调用 [在您的个人资料中支持] StreamReader(Stream) 构造函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多