C#如何使用File.ReadLines读取嵌入式资源文本文件?

时间:2016-07-02 12:07:26

标签: c# resources

我该怎么做? 我的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");

但不是资源!

这让我很生气

1 个答案:

答案 0 :(得分: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();