Reputation: 4004
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!
Upvotes: 21
Views: 56795
Reputation: 6455
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..
Upvotes: 29
Reputation: 21
The simple and straight forward method is -
var keyValue = Resources.ResourceManager.GetString("AboutTitle");
In the above "AboutTitle" is the "KEY" part so if you have in the resource "AboutTitle= About" the above result for keyValue = About
I have used this in one of my project which is in VS2019.
Upvotes: 2
Reputation: 1339
Here is an limited example that:
.
public static string GetLocalizedNameReversed(string value)
{
ResourceManager rm = new ResourceManager($"YourNamespace.YourFolder.YourResourceFileName", assembly);
var entry = rm.GetResourceSet(new CultureInfo("nb-NO"), true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(e => e.Value.ToString().Equals(value, StringComparison.OrdinalIgnoreCase));
return entry.Key.ToString();
}
Upvotes: 0
Reputation: 19
You could use this inline code (c#, ASP.NET MVC)
var _resource = new ResourceManager(typeof(/*your resource*/));
string res = _resource.GetString(/*resource key*/);
i think this could help u.
Upvotes: 0
Reputation: 39
public static class ResourceManagerHelper
{
public static string GetResourceName(this ResourceManager resourceManager, string value, CultureInfo cultureInfo, bool ignoreCase = false)
{
var comparisonType = ignoreCase ? System.StringComparison.OrdinalIgnoreCase : System.StringComparison.Ordinal;
var entry = resourceManager.GetResourceSet(cultureInfo, true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString().Equals(value, comparisonType));
if (entry.Key == null)
throw new System.Exception("Key and value not found in the resource file");
return entry.Key.ToString();
}
}
To Call this extension method,
var key = Resources.ResourceManager.GetResourceName(value, CultureInfo.InvariantCulture, true);
In this case, we don't want to pass the resource assembly, rather we can invoke using the particular resource's resourceManager.
Upvotes: 1
Reputation: 303
Just in case it might help anyone. This ResourceHelper is inspired by jure and Mohan Singh Saini.
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;
public class ResourceHelper
{
/// <summary>
/// ResourceHelper
/// </summary>
/// <param name="resourceName">i.e. "Namespace.ResourceFileName"</param>
/// <param name="assembly">i.e. GetType().Assembly if working on the local assembly</param>
public ResourceHelper(string resourceName, Assembly assembly)
{
ResourceManager = new ResourceManager(resourceName, assembly);
}
private ResourceManager ResourceManager { get; set; }
public string GetResourceName(string value)
{
DictionaryEntry entry = ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true).OfType<DictionaryEntry>().FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString() == value);
return entry.Key.ToString();
}
public string GetResourceValue(string name)
{
string value = ResourceManager.GetString(name);
return !string.IsNullOrEmpty(value) ? value : null;
}
}
Upvotes: 2
Reputation: 74
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 "";
}
}
Upvotes: 5