使用StreamReader打开资源文件?

3

这个不起作用:

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();

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

2
"doesn't work" 的意思是什么?它会抛出异常吗?还是默默失败? - Andrew Hare
2
查看:https://dev59.com/rm435IYBdhLWcg3wkA5e#5343005 - Arnaud F.
资源文件通常是二进制文件。使用 StreamReader 读取它可能无法提供您想要的信息。请参阅 @Arnaud F. 提供的答案以从流中读取文本资源。 - Jim Mischel
3个回答

5
试试这样做:
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];
    }
}

我有一个名为security.file的文件。它是一个文本文件,当我将Resource.text分配给fileContent时,它是一个byte[],不能隐式转换为字符串。 - Cocoa Dev

0

要读取资源,你需要一个名为“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;
    }
}

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接