Loading Resources

Last modified August 12, 2014

Required

Tutorials:  First NETMF Project

Introduction

A resource is a file that is included with the application deployment. If the application depends on a file (images, icons, sounds, strings, or binary files) then we may want to add this file to the application's resources. Applications could read the file from the file system but then the application becomes dependent on the file system to run.

Always know how much memory is reserved for the application deployment. Adding a file that takes up more space than the space available for deployment will result in errors deploying.

Looking in the “Solution Explorer” window, we see the “Resource.resx” and if we expand it we see “Resources.Designer.cs”. Visual Studio automatically generates this file to support resources. Never try to modify this file manually.

Instead, double click “Resources.resx” to edit the resources using a tool.

On the resource editor window, the first drop-down list on the left is to select what type of resources we want to edit. You notice how there is already one “string resource” named “String_1” With its “value” set to “Hello World!” Click right below “String_1” and add a new string resource as shown in image below.

  

Using Resources

We now have two string resources. The below code shows you how to access them.

Debug.Print(Resources.GetString(Resources.StringResources.String1));
Debug.Print(Resources.GetString(Resources.StringResources.String2));
Warning

Visual Studio places the generated resources under the default project namespace. Make that that namespace is in score when you try to access the Resources class.

Try modifying the string resource text and observe how the output changes. Let us add a file. Create a text file on your desktop and put some text in it. Select “files” from the left drop-down menu then clock on “Add Resource...”

Since this is a text file, Visual Studio is smart enough to add this in a similar way to strings.

Reading a Text File

So to use this resource file, we access it the same way we access a string.

var str = Resources.GetString(Resources.StringResources.SomeFile);

Resource Size

The file labamba is about 33Kb (see below). Which is small in the computer world but for embedded systems it is quite large. The example below will work on devices with a lot of RAM. If you need a resource that is too large to load at once, try breaking it up into chunks before adding it and then processing each chunk individually.

Again, you access binary resources in the same way except that you are given a byte array instead.

byte[] file_data = Resources.GetBytes(Resources.BinaryResources.labamba);
Tip

After adding new resources via the resources designer save your changes by right clicking on the Resources.resx tab and choosing Save or by typing CTRL-S. This makes sure that Visual Studio updates other relevant files such as Resources.Designer.cs.

Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.

* Indicates required fields.
This form is only for feedback not support.
Review our how to find information guide on locating helpful resources.