C# 是否使用StreamReader打开资源文件?

C# 是否使用StreamReader打开资源文件?,c#,C#,这不起作用: string fileContent = Resource.text; StreamReader read = File.OpenText(fileContent); string line; char[] splitChar = "|".ToCharArray(); while ((line = read.ReadLine()) != null) { str

这不起作用:

string fileContent = Resource.text;
    StreamReader read = File.OpenText(fileContent);

    string line;
            char[] splitChar = "|".ToCharArray();

            while ((line = read.ReadLine()) != null)
            {
                string[] split = line.Split(splitChar);
                string name = split[0];
                string lastname = split[1];

            }

            read.Dispose();

如何打开资源文件以获取其内容?

我认为变量fileContent已经包含了您所需的所有内容。

尝试如下:

string fileContent = Resource.text;
using (var reader = new StringReader(fileContent))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        string[] split = line.Split('|');
        string name = split[0];
        string lastname = split[1];
    }
}

要读取资源,您需要一个名为“ResourceReader”的特殊流,您可以这样使用它:

string fileContent = "<your resource file>";

using (ResourceReader reader = new ResourceReader(fileContent))
{
    foreach (IDictionaryEnumerator dict in reader)
    {
        string key = dict.Key as string;
        object val = dict.Value;
    }
}
string fileContent=“”;
使用(ResourceReader=新的ResourceReader(fileContent))
{
foreach(读卡器中的IDictionaryEnumerator dict)
{
字符串键=作为字符串的dict.键;
对象值=默认值;
}
}

什么是“不起作用”?它会引发异常吗?它会自动失败吗?请参阅:资源文件通常是二进制文件。使用
StreamReader
阅读可能不会提供您想要的信息。请参阅@Arnaud F.为从流中读取文本资源提供的答案。我有一个名为security.file的文件。它是一个文本文件,当我将Resource.text分配给fileContent时。它是字节[],不能隐式转换为字符串