Main Site Documentation

Using a resource file


#1

Hello,

I’d like to use a resource file and want to read values from it during runtime.

Now I got something like:


using System;
using System.Collections;
using System.Threading;
using System.Resources;
using System.Reflection;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace MFConsoleApplication1
{
    public class Program
    {   
        public static void Main()
        {
            ResourceManager rm = new ResourceManager("Resources", Assembly.GetExecutingAssembly());

         }
    }
}


I’d like to do something like:

Debug.print(rm.GetString("string1"));

But that won’t work…
Any suggestions?

Thanks,
Jeroen


#2

There is an examples of how to loading the resource here:

http://www.microframeworkprojects.com/index.php?title=Loading_Resources

Hope this help.


#3

Hi Sam,

Thanks for your reply. I’ve also found that one, but I can only read it like:


Resources.GetString(Resources.StringResources.String1)

Where the string is fixed in the code.

But I want something like:


string sKey="string1";
Resources.GetString(Resources.StringResources(sKey))

Hullie


#4

Well, It works fine with me!

Here is the code:

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

namespace FEZ_Domino_Application1
{
    public class Program
    {
        public static void Main()
        {
            // Blink board LED

            bool ledState = false;

            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(500);

                // toggle LED state
                ledState = !ledState;
                led.Write(ledState);

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

            }
        }

    }
}

#5

Hi Sam,

That’s working for me as well, but is not what I want.
I Want to get a specific resource, which name is inside a string.
So not Resources.StringResources.String1
string key=“string1”;
But something like Resources.StringResources(key)

Regards,
Hullie


#6

Hullie,

That is not how it works. Each resource is given an id which is a short number.

When you add a resource VS adds a value to an enumeration that makes easy for you to access the resource:

Open Resources.Designer.cs. You might find there something like this:

[System.SerializableAttribute()]
        internal enum StringResources : short
        {
            String2 = 1225,
            String1 = 1228,
        }

Visual Studio will add separate enumeration for each type of resource. But again the key is a short value.

Remember it is about “thinking small”. With limited resources it makes sense to use a short value for the key instead of a string.

May I ask why are you insisting on using strings for resource keys other than using what you have for free?


#7

AFAICT, you have to cook that yourself, because we don’t have Enum.GetNames.
Basically, create a switch method that returns the Enum from a name and use the Enum.

private static Properties.Resources.StringResources GetByName(string name)
{
    switch (name.ToLower())
    {
        case "string1":
            return Properties.Resources.StringResources.String1;
        default:
            throw new InvalidOperationException("Invalid resource name");
    }
}

string s = Properties.Resources.GetString(GetByName("string1"));
Debug.Print(s);

#8

Hi William,

Ok thanks!
That’s the way I’m doing it now.
I’ve to explicitly name all the keys which are in the resource file. :frowning:
But, it’s working :slight_smile:

Regards,
Hullie