1

按照我没有将文件“text.txt”放入资源文件时使用的代码:

System.IO.StreamReader file = new System.IO.StreamReader("text.txt");

现在文件“text.txt”在资源文件中,这段代码给了我错误。怎么解决?

4

1 回答 1

3

If you add a Textfile to your Resources, than you can get the Content of this Textfile as String via Properties.Resources:

string textFileContent = Properties.Resources.NameOfYourResource

You could also make a Property to access your the Content of your ResourceFile:

public string YourResource
{
    get
    {
        return Properties.Resources.NameOfYourResource
    }
}

If you want to read your ResourceFile Line by Line or mabe only the first line:

string text = Properties.Resources.text;

        using(TextReader sr = new StringReader(text))
        {
            var firstline = sr.ReadLine();
            Console.WriteLine("FIRSTLINE: " + firstline);

            string line;
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
于 2013-10-03T18:13:01.717 回答