Reputation: 3
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
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