RobertKing
RobertKing

Reputation: 1911

Access the value from resource file programmatically

Came across a situation that requires accessing string value stored in resource file based on the key provided from code behind.

how can i do this?

Note: the resource file exist in a web project in my solution which i want to access from my silverlight application..

Upvotes: 19

Views: 41158

Answers (6)

agileDev
agileDev

Reputation: 441

If you have multiple ".resx" files of different languages under "Properties" and dynamically change the resource file. Then you get the value of a particular key from code behind like this. You don't have to mention the ".resx" file name. It will automatically get the value from the currently set ".resx" file (culture):

string str=[ProjectName].Properties.Resources.[ResourceKeyName]

Upvotes: 1

Kshitij Jhangra
Kshitij Jhangra

Reputation: 607

Assume that your resource file name is "Resource.resx", and you want to pass key dynamically then,

using System.Resources;
string resVal = Resource.ResourceManager.GetString(dynamicKeyVal);

Let me know if it not works, I will work on it and will provide the appropriate solution.

Upvotes: 10

Monojit Sarkar
Monojit Sarkar

Reputation: 2451

i generally access resource like this way

Resources.MyLocalised.CompanyName;

Upvotes: 5

Software Engineer
Software Engineer

Reputation: 3956

Try using:

string resourceValue = HttpContext.GetGlobalResourceObject("resxFilename", "resourceKey").ToString();

Upvotes: 1

Thilina H
Thilina H

Reputation: 5804

You can use as following no need to create ResourceManager instance.Resource file already has inherited from ResourceManager.

string value = MyResource.ResourceManager.GetString("stringKey");

Note:MyResource is shared resource file Name.

Upvotes: 3

Zbigniew
Zbigniew

Reputation: 27584

You can use ResourceManager class:

ResourceManager myManager = new ResourceManager(typeof(MyResource));
string myString = myManager.GetString("StringKey");

Upvotes: 28

Related Questions