Tuesday, May 5, 2009

Read Resource file: Resource.resx dynamically thourgh C#

Dear Reader,

Following is a sample method to read a Resource file in your web application which resides in the location "App_GlobalResources\Resource.resx". The assembly references needed are

using System.Collections;

using System.Resources;

Is case if there is a message shown when solution is built

"The type or namespace name 'ResXResourceReader' does
not exist in the class or namespace 'System.Resources' (are you missing an
assembly reference?)"

Please refer ResXResourceReader missing an assembly reference

Source code:

public void ReadResourceresx()


//Creating a hashtable to store string in resource files

Hashtable resourceStringsHashTable = new Hashtable();

//Provide relative path of Resource file

string strResourceFilelocation = HttpContext.Current.Request.PhysicalApplicationPath.ToString() +


//Creating object for Class: ResXResourceReader

System.Resources.ResXResourceReader objResXResourceReader = null;



objResXResourceReader = new ResXResourceReader(strResourceFilelocation);

//Defines a DictonartEntry Key/Value to Set or retriev values

foreach (DictionaryEntry resourceString in objResXResourceReader)


if (!resourceStringsHashTable.Contains(resourceString.Key.ToString()))


//Adding values to HashTable one by one in a loop

resourceStringsHashTable.Add(resourceString.Key.ToString(), resourceString.Value);






//Closing the created object




Hope it helps :)



No comments: