What's Here?
- Members: 300,308
- Replies: 825,514
- Topics: 137,361
- Snippets: 4,417
- Tutorials: 1,147
- Total Online: 2,101
- Members: 128
- Guests: 1,973
|
This is a snippet you can use to read a certain value from a resource file you have in your application (.resx extension)
|
Submitted By: PsychoCoder
|
|
Rating:

|
|
Views: 39,433 |
Language: C#
|
|
Last Modified: January 22, 2008 |
|
Instructions: Pass the method the name of the file you want to read from, and the key you want the value for and it returns the value |
Snippet
//Namespace reference
using System;
using System.Resources;
#region ReadResourceFile
/// <summary>
/// method for reading a value from a resource file
/// (.resx file)
/// </summary>
/// <param name="file">file to read from</param>
/// <param name="key">key to get the value for</param>
/// <returns>a string value</returns>
public string ReadResourceValue(string file, string key)
{
//value for our return value
string resourceValue = string.Empty;
try
{
// specify your resource file name
string resourceFile = file;
// get the path of your file
string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
// create a resource manager for reading from
//the resx file
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
// retrieve the value of the specified key
resourceValue = resourceManager.GetString(key);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
resourceValue = string.Empty;
}
return resourceValue;
}
#endregion
Copy & Paste
|
|
|
|