1

Following my code I used when I did not put the file "text.txt" in the resource files:

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

Now the file "text.txt" is in the resource files, this code it gives me error. How to solve?

user2843341
  • 283
  • 2
  • 3
  • 8

1 Answers1

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);
            }
        }
makim
  • 3,154
  • 4
  • 30
  • 49
  • It gives me error as "file not found". Where did I put Properties.Resources? – user2843341 Oct 03 '13 at 18:20
  • 1
    very strange, how did you add the resource? – makim Oct 03 '13 at 18:25
  • The name of resources is: Resources. – user2843341 Oct 03 '13 at 18:28
  • Under your ProjectDirectory there is a Folder called "Resources" is your text.txt file in there? – makim Oct 03 '13 at 18:28
  • @user2843341 sine's answer is correct, go to project -> properties -> resource and look there how you call the file. – Alessandro D'Andria Oct 03 '13 at 18:31
  • Ok, but I have not put Properties.Resources, because I do not know where to place it. – user2843341 Oct 03 '13 at 18:33
  • what do you mean by "you have not put Properties.Resources"!?! – makim Oct 03 '13 at 18:35
  • you should place it there where you need the content of the textfile?!? It´s just one line of code you can put it anywhere, as long as it is in a class or method! – makim Oct 03 '13 at 18:38
  • Is correct: string textFileContent = Properties.Resources.("text.txt"); System.IO.StreamReader file = new System.IO.StreamReader(textFileContent);? – user2843341 Oct 03 '13 at 18:40
  • no this is not correct why do you need a StreamReader? And you NEVER can do something like Resources.("text.txt"), this want even compile! If you want the content of this file just put the code from my answer where you need the filecontent or do you need the filecontent in form of a StreamReader? – makim Oct 03 '13 at 18:44
  • In summary, If I have two files: "a.txt" and "b.txt", how do I read ONLY to b.txt? – user2843341 Oct 03 '13 at 18:52
  • if you have added both as resource to your project than via Properties.Resources.b – makim Oct 03 '13 at 18:53
  • Perhaps we have not understood, how do I show the first line of b.txt in console? – user2843341 Oct 03 '13 at 18:59
  • updated the answer, but don´t forget to add the file as a resource! – makim Oct 03 '13 at 19:07