Right now I have a handful of those in a class:
string upgrade_file
= new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(
Assembly.GetExecutingAssembly().GetName().Name
+ ".schemas.template.db_upgrades.txt")
).ReadToEnd();
I can write a wrapper function to simplify accessing embedded resources (and I will if I have to), but is there a simpler or more elegant way to access them natively with .Net?
For example, I find it strange that GetManifestResourceStream(...) isn't static. Another example: is there some method that returns a string rather than a stream for text files?
UPDATE 1:
To be clear, I have text files in sub-directories, and I want that:
- These files remain separate files (for example to be able to source control them separately)
- These files remain embedded resources, i.e. compiled with the assembly.
Right now I'm doing this as shown in this image:
UPDATE 2:
I did not manage to use anything from the answers to simplify accessing the files.
Here is the helper method I'm now using in case someone else might find it useful:
/// <summary>
/// Get the content of a text file embedded in the enclosing assembly.
/// </summary>
/// <param name="resource">The "path" to the text file in the format
/// (e.g. subdir1.subdir2.thefile.txt)</param>
/// <returns>The file content</returns>
static string GetEmbeddedTextFile(string resource)
{
return new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(
Assembly.GetExecutingAssembly().GetName().Name + "." + resource)).ReadToEnd();
}