3

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:

  1. These files remain separate files (for example to be able to source control them separately)
  2. These files remain embedded resources, i.e. compiled with the assembly.

Right now I'm doing this as shown in this image: enter image description here

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();
}
  • 1
    You can look at the [ResourceManager](http://msdn.microsoft.com/en-us/library/System.Resources.ResourceManager.aspx) class. – Scorpion-Prince Apr 12 '12 at 21:25
  • all about resources http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net – Antonio Bakula Apr 12 '12 at 21:33
  • i'm guessing that EF does this same thing with the XML mappings, based on the way the connection string looks - i wonder if there's a reflector gem somewhere in there... – Aaron Anodide Apr 12 '12 at 21:53

4 Answers4

0

I have a resource file in my project named Resource1 containing a TXT file and a binary file.
I'm able to do

string str = Resource1.TxtFile;
byte[] file = Resource1.BinaryFile;
Marco
  • 56,740
  • 14
  • 129
  • 152
  • @sixfeetsix: I'm not sure my edited answer is what you're searching for, anyway take a look – Marco Apr 12 '12 at 21:36
  • anyway I can do something similar and keep my each resource in separate files? –  Apr 16 '12 at 10:20
  • I just tried using resources and realized that: 1- adding a file resource puts the file in the Resources directory whose changes are then tracked; 2- the resource is still embedded in the .dll file; that corresponds to what I want. –  May 22 '12 at 09:30
0

Did you know that in VS if you have a Resources.resx file, it generates code that is a real class? You can look at it and use it directly without any meta operations.

If you double click the Rsources.resx file and open the designer, you can find a dropdown that changes the access for the generated class from internal to public if that is required for your purpose.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • I want to keep resources such as the one described in separate files for practical reasons, mainly that it allows me to source control them separately. –  Apr 12 '12 at 21:35
0

My personal approach is to write an extension for the Assembly class, because this seems like a method that should have been included in that class anyway.

So, as mentioned above, first make sure your text file is marked as "Embedded Resource", then use code similar to the following:

public static class Extensions
{
    public static string ReadTextResource(this Assembly asm, string resName)
    {
        string text;
        using (Stream strm = asm.GetManifestResourceStream(resName))
        {
            using (StreamReader sr = new StreamReader(strm))
            {
                text = sr.ReadToEnd();
            }
        }
        return text;
    }
}

This allows you to load it from a DLL or whatever assembly you wish with code something like the following:

        string content = Assembly.GetExecutingAssembly().ReadTextResource(myResourceName);

(The above could be coded more succinctly, but I went with this for the purposes of demonstration)

jrichview
  • 338
  • 2
  • 14
-2
string upgrade_file  = Resources.db_upgrades.txt
clumpter
  • 1,898
  • 6
  • 27
  • 38