Lior.Net


How To Work with Resources Files (Resx + resource) in C#

Many companies are working with resource files (.resx or .resource) in order to support multi-lingual Application \ Website. In the following Article I will demonstrate how to work with each kind.

According to the user culture, we can easily show our App \ Website in the proper language.

For Example:

// The following function return value of a key according to specific culture

Private string GetKeyValue(string filename, string resourcePath,string key,string culture)

{

ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null); Directory.GetCurrentDirectory(), null);

// retrieve the value of the specified key

String value = rm.GetString(key, CultureInfo.CreateSpecificCulture(culture));

if (!String.IsNullOrEmpty(value))

return value;

else

return null;

Resource Files

Read Resource File

private void ReadResourceFile(string filename)

{

// Read Entire File

IResourceReader reader = new ResourceReader(filename);

// Iterate through the resources and so some action

foreach (DictionaryEntry d in reader)

{

// Do Action

}

}

Write Resource File

private void WriteResourceFile(string filename)

{

IResourceWriter writer = new ResourceWriter(filename);

// Add Resources “Identifier” , value

writer.AddResource(”Identifier”, “value”);

writer.AddResource(”Identifier”, “value”);

// Close the file

writer.Close();

writer.Dispose();

Resx Files

Read Resx File
private void ReadResFile(string FileName)

{

ResXResourceReader rsxr = new ResXResourceReader(FileName);

// Iterate through the resources and do some action

foreach (DictionaryEntry d in rsxr)

{

// Do Action

}

}

Write Resx File

private void WriteResFile(string FileName)

{

ResXResourceWriter rsxr = new ResXResourceWriter(FileName);

// Add Resources

rsxr.AddResource(”Identifier”, “value”);

rsxr.AddResource(”Identifier”, “value”);

// Close the file

rsxr.Close();

rsxr.Dispose();

}

Tags: , , ,

Leave a Reply

Jajah is the VoIP player that brought you web-activated telephony.