【问题标题】:C# How to read embedded resource text file with File.ReadLines?C# 如何使用 File.ReadLines 读取嵌入的资源文本文件?
【发布时间】:2016-07-02 12:07:25
【问题描述】:

我该怎么做? 我的 hello.txt 字符串是:

string hello = Properties.Resources.hello;

我想要这样:

Random rand = new Random();
IEnumerable<string> lines = File.ReadLines(hello);
var lineToRead = rand.Next(1, lines.Count());
var line = lines.Skip(lineToRead - 1).First();
txtbx_output.Text = line.ToString();

这对我来说没有问题:

IEnumerable<string> lines = File.ReadAllLines(@"my pathblabla\Text\hello.txt");

但不是作为资源!

这让我很生气

【问题讨论】:

  • hello的值是多少?
  • 我已将 hello.txt 添加为资源。 hello 里面只有两个词,例如:hi 和 hello。然后点击一个按钮后,这些词应该出现......它不起作用我这让我很生气
  • 需要使用 file.ReadLines 吗?
  • 资源是编译后的二进制文件,不是文本。
  • File.ReadLines(string path),您需要指定路径,但您指定的是string hello。如果您需要使用File.ReadLines(),那么您需要使用反射找到该资源的路径。

标签: c# resources


【解决方案1】:

试试这个:

string hello = Properties.Resources.hello;
Random rand = new Random();
IEnumerable<string> lines = hello.Split(new[] { Environment.NewLine }, 
                                StringSplitOptions.RemoveEmptyEntries).ToList();
var lineToRead = rand.Next(1, lines.Count());
var line = lines.Skip(lineToRead - 1).First();
txtbx_output.Text = line.ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2013-03-21
    • 2012-04-19
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多