What's Here?
- Members: 148,748
- Replies: 504,863
- Topics: 79,502
- Snippets: 2,661
- Tutorials: 705
- Total Online: 1,702
- Members: 74
- Guests: 1,628
|
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: 16,095 |
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
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|