Wednesday, December 26, 2007

How to read strings from a resource (resx) file in C# ?

Following is a source code that demonstrates How to Read a resource string from a Resx file. Here it is assumed that the resource file i.e. Sample.resx contains resource strings.
public void ReadResourceStrings()
{
//This hashtable will cache all the resource strings।
Hashtable resourceStringsHashTable = new Hashtable();
//Complete path of the resource file.
string resourceFilePath = "Sample.resx";
if (String.IsNullOrEmpty(resourceFilePath)
!File।Exists(resourceFilePath))
{throw (new Exception("Resource file cannot be found!"));}
ResXResourceReader resourceFileReader = null;
try
{
resourceFileReader = new ResXResourceReader(resourceFilePath);
foreach (DictionaryEntry resourceString in resourceFileReader)
{
if (!resourceStringsHashTable.Contains(resourceString.Key.ToString()))
{
resourceStringsHashTable.Add(resourceString.Key.ToString()
, resourceString.Value);
}
}
}
finally
{
resourceFileReader.Close();
}
}

1 comments:

Anonymous said...

Hi Dude,

thx for the ResXResourceReader Idea!

Wp9omp

Post a Comment