Using Resources for External Content - Page 2
       by kirupa  |  12 January 2007

In the previous page, you learned about resources and what they try to do. We also started learning about using resources in our applications, so let's pick up from where we left off.

Changing Build Action
By default, when you publish your application, your external files are kept separate from your final application. For what we are trying to do, we want our external file to be a part of the executable itself, and we can do that by changing our file's Build Action.

To change the Build Action, select your newly imported file in your Solution Explorer. Once you have selected the imported file, which in my case is blue.png, take a look at the Properties grid panel:

[ select your imported file and take a look at your Properties grid panel ]

Notice that there is an entry for Build Action. Select the Build Action entry, and  when you select it, you should see a drop-down arrow appear to the right of the Content text. Click on that arrow and select Embedded Resource:

[ change your Build Action to Embedded Resource ]

By tagging your file as an embedded resource, you tell Visual Studio to include this file as part of the assembly instead of referencing this as a separate file.

Referring to Embedded Resources using Code
With your file imported and tagged as an embedded resource, the final step is to use the embedded resource in your application. Since I've been using blue.png as an example file for the past few sections, I'll continue using that file in my example.

If I wanted to add blue.png to a WinForms button called btnSubmit, I would use the following code:

btnSubmit.Image = Image.FromStream(Assembly.GetExecutingAssembly
().GetManifestResourceStream("ButtonIcon.blue.png"));

Let's look at the above code in detail. Because I want to insert an image into my application, I use the Image class's FromStream method (Image.FromStream) which takes a stream as its argument. The reason I am looking for a method that takes a stream as an argument is because that is the main format my embedded resource will be accessible to the application.

Moving on, the Assembly class allows you to explore the various metadata associated with your program. You use the Assembly class's GetExecutingAssembly method to return an Assembly object that points to the assembly that is currently running. In other words, you are trying to find a way to explore the metadata associated with your current program!

Now that you have access to your assembly by using Assembly.GetExecutingAssembly(), the next step is to get the manifest from this assembly. More importantly, beyond just the manifest, we also want the resource the manifest provides access to. We do that using the GetManifestResourceStream() method and passing a string to the internal location of the file.

The internal location of the file follows the Namespace.filename.extension format. In my example, the namespace under which I will be accessing my blue.png resource is called ButtonIcon, and the file's name is blue, and the extension is png. Putting it all together, we get: ButtonIcon.blue.png. That's all there is to it.

Because I feel one example for something this complicated is not adequate, I have also provided the code I used to access an Embedded Resrouce text file called words.txt:

TextReader tr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("IncrementalSearch.words.txt"));

Notice that in the above example, I am using another stream method - except on that supports text files such as StreamReader. The namespace of that particular application is called IncrementalSearch, and the name of the file I am accessing is words.txt.

Onwards to the next page!

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.