user4355456
user4355456

Reputation: 3

Read txt files from build without writing them

I want to make a console application which tests a .txt file to be included with the application. However, File.ReadAllLines(string) seems to have only the string-filepath constructor and not one taking the file itself, which could be accessed by mytextfile = myProject.Properties.Resources.mytextfile.txt;

so do I have to write the file from the build somewhere, call reflection on it to find out its full path and only then be able to use ReadAllLines() to convert it into a string-array? is there no easier way to do this?

Upvotes: 0

Views: 53

Answers (1)

mclaassen
mclaassen

Reputation: 5138

You can open the embedded resource as a stream like this:

Assembly assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("Namespace.mytextfile.txt");

and then get the text from the stream like this:

var reader = new StreamReader(stream);
var text = reader.ReadToEnd();

Upvotes: 2

Related Questions