从文本文件添加到列表<string>

Jas*_*n D 2 .net c# io

我有一个List<string>我希望通过设置为项目资源的文本文件填充.我已经看了一下这样做的方法,但还没有发现一个不会导致我的程序崩溃的方法.

如果我手动填充列表...

_names.Add("Sam");
_names.Add("John");
_names.Add("Mike");
Run Code Online (Sandbox Code Playgroud)

一切正常 我的文本文件在单独的行上有每个名称,没有逗号或任何东西.当我尝试读取名称时,无论我走哪条路线,程序都会崩溃.这是我尝试过的最新方式,不过还有很多其他方法:

using (var reader = new StreamReader(Properties.Resources.sampleNamesMale))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        _names.Add(line);
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,我无法隔离崩溃的原因,因为每次崩溃都会提到ViewModelLocator,这与此问题完全无关.

有没有人对如何解决这个问题有任何想法?我当然感谢任何建议.

更新: Try-catch不会产生任何结果.这是我得到的错误:

发生了XamlParseException - '对类型'AoW.ViewModels.ViewModelLocator'的构造函数的调用与指定的绑定约束相匹配引发了异常.行号"13"和行位置"10".

它指向我主窗口构造函数中的InitializeComponent().

更新2:真正的例外是:

"发生了ArgumentException - 路径中的非法字符." 它指向使用(var reader .... line.

fcu*_*sta 5

使用StringReader而不是StreamReader:

using (var reader = new StringReader(Properties.Resources.sampleNamesMale))
Run Code Online (Sandbox Code Playgroud)

您收到该错误,因为StreamReader(字符串)需要文件路径.如果要在Properties.Resources.sampleNamesMale中提供实际文本,则必须使用StringReader.