Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Name by string Value from a .NET resource (RESX) file

Tags:

c#

.net

resx

Here's how my RESX file look like:

Name            Value        Comments
Rule_seconds    seconds      seconds
Rule_Sound      Sound        Sound

What I want is: Name by string Value, something like below:

public string GetResxNameByValue(string value)
{
// some code to get name value
}

And implement it like below:

string str = GetResxNameByValue("seconds");

so that str will return Rule_seconds

Thanks!

like image 615
Learner Avatar asked May 03 '13 14:05

Learner


2 Answers

This could work

private string GetResxNameByValue(string value)
    {
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager("YourNamespace.YourResxFileName", this.GetType().Assembly);


        var entry=
            rm.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true)
              .OfType<DictionaryEntry>()
              .FirstOrDefault(e => e.Value.ToString() ==value);

        var key = entry.Key.ToString();
        return key;

    }

With some additional error checking..

like image 67
Jurica Smircic Avatar answered Oct 25 '22 18:10

Jurica Smircic


you can access directly by passing key:

    public  string gtresource(string rulename)
    {
        string value = null;
        System.Resources.ResourceManager RM = new System.Resources.ResourceManager("CodedUITestProject1.Resource1", this.GetType().Assembly);
        value = RM.GetString(rulename).ToString();

        if(value !=null && value !="")
        {
            return value;

        }
        else
        {
            return "";
        }

    }
like image 35
Mohan Singh Saini Avatar answered Oct 25 '22 18:10

Mohan Singh Saini