Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the value from resource file programmatically

Tags:

c#

asp.net

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..

like image 416
RobertKing Avatar asked Oct 19 '13 11:10

RobertKing


People also ask

How do you retrieve local resources programmatically?

Call the GetLocalResourceObject or GetGlobalResourceObject method to read specific resources from a global or local resource file, respectively. These overloaded methods are available in the HttpContext and TemplateControl classes.

How do I access resource files in Visual Studio?

Open your project in Visual Studio and navigate to Solution Explorer. Expand the Resource Files folder, then: To open in the text editor, double-click the . manifest file.

What is Resx?

resx resource file format consists of XML entries that specify objects and strings inside XML tags. One advantage of a . resx file is that when opened with a text editor (such as Notepad) it can be written to, parsed, and manipulated.


2 Answers

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.

like image 76
Kshitij Jhangra Avatar answered Oct 18 '22 23:10

Kshitij Jhangra


You can use ResourceManager class:

ResourceManager myManager = new ResourceManager(typeof(MyResource));
string myString = myManager.GetString("StringKey");
like image 41
Zbigniew Avatar answered Oct 18 '22 23:10

Zbigniew