1

I have a text file data.txt that's embedded in my solution (as described in this SO question).

How do I read the contents of this file as a string? I'm imagining something like this:

 string data = Resources["data.txt"];

but that's not the way to do it.

Community
  • 1
  • 1
Joe
  • 3,804
  • 7
  • 35
  • 55
  • Beyond the many duplicates, the answer to your question is also found in one of the answers for the question you linked. – EricLaw May 18 '13 at 04:18
  • It looks like the answer there requires the resource file to be exported to the target folder. Am I missing something here? – Joe May 18 '13 at 04:20
  • @Joe: I don't think it does. Look at [this Microsoft KB article](http://support.microsoft.com/kb/319292), linked from an answer in that question. – icktoofay May 18 '13 at 04:21

1 Answers1

2

If you add the file as a resource you should be able to access it like this:

Properties.Resources.data

Or alternatively if you set the Copy to Output Directory property to Copy always/Copy if newer, you can do something like:

using (FileStream fs = System.IO.File.Open("Resources/data.txt", FileMode.Open))
{
   // do amazing stuff here ...            
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79