0

When I add a txt file as a resource to a project, how can I then consume the contents of that resource as a string?

The closest I've been able to get is by using the Resource Manager, to pull an unmanaged stream. However, this throws a null error:

using (StreamReader sr = new StreamReader(
    Properties.Resources.ResourceManager.GetStream(
        "TestFile.txt", CultureInfo.CurrentCulture)))
{
    Console.WriteLine(sr.ReadToEnd());
}
leppie
  • 115,091
  • 17
  • 196
  • 297
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • Are you sure it's not an Embedded Resource (as opposed to a Resource)? If it is, you'd want to use `GetManifestResourceStream` – stephen.vakil Apr 12 '16 at 18:52
  • 1
    Additional to stephens comment: [link](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Klatschen Apr 12 '16 at 18:54
  • 1
    I just added a text file named `test.txt` as a resource, and it added it as a `String` to `Properties.Resources.test`. So I'm wondering what you did differently.. I just went to the project properties, then `Resources`->`Add Resource`->`Add Existing File`. After that I don't have to "consume the contents as a string", it *is* a string already. – Quantic Apr 12 '16 at 18:56
  • This is a resource I added through the project's Properties / Resources tab. It is not currently added to the project as an embedded resource, although I could do that. – Aaron Thomas Apr 12 '16 at 19:09
  • In that case I assume you can just do, `Console.WriteLine(Properties.Resources.TestFile)` and it will write out the whole file, no `StreamReader` needed. According to my test, you already have a `string` object that is the whole file and the object resides under `Properties.Resources.TestFile`. I thought that was clear from my first comment, my mistake. I also assume this "embedded resource" stuff *would* mean that you need to `StreamReader` it because then it's an actual file, not a `string`. – Quantic Apr 12 '16 at 19:16
  • @Quantic thank you, that pushed me to the answer. – Aaron Thomas Apr 12 '16 at 19:23
  • 1
    This is not a coding question about Visual Studio. Dont tag a tool. – leppie Apr 12 '16 at 19:26
  • Possible duplicate of http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Will Ray Apr 12 '16 at 19:31

2 Answers2

1

You could do this too:

var myAss = Assembly.GetExecutingAssembly();
var mytxtFileResource = "Namespace.Project.MyTxtFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(mytxtFileResource))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}
pantuofermo
  • 160
  • 7
0

You dont need to do it like that for text files Just write it like this https://msdn.microsoft.com/en-us/library/aa287548(v=vs.71).aspx

System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);
file.Close();

and read it like this: https://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx

int counter = 0;
string line;

// Read the file and display it line by line.

System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();
ash
  • 21
  • 6