Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from resource file using its key

Tags:

c#

How to get a value from resource file using its key

like image 700
Kuttan Sujith Avatar asked May 10 '11 07:05

Kuttan Sujith


3 Answers

There is a much easier way: Namespace.Properties.Resources.FileName -> gets the string of the file-content.

i.e.: TestProject1.Properties.Resources.MyXmlFile -> direct access to the File in the resources

like image 169
pendl Avatar answered Oct 24 '22 03:10

pendl


ResourceManager.GetString or ResourceManager.GetStream, depending on the type of the resource.

like image 25
Anders Lindahl Avatar answered Oct 24 '22 02:10

Anders Lindahl


public string ReadResourceValue(string file, string key)

{

    string resourceValue = string.Empty;
    try
    {

        string resourceFile = file;

        string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

        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;
}
like image 39
safi Avatar answered Oct 24 '22 02:10

safi