How to Get Name by String Value from a .Net Resource (Resx) File

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

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..

read string from .resx file in C#

This example is from the MSDN page on ResourceManager.GetString():

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly());

// Retrieve the value of the string resource named "welcome".
// The resource manager will retrieve the value of the
// localized resource using the caller's current culture setting.
String str = rm.GetString("welcome");

Get value from resx in Class Library

Try like this:

ResourceManager resourceManager = 
new ResourceManager("Resources.xxx", Assembly.Load("App_GlobalResources"));
string myString = resourceManager.GetString("StringKey");

where xxx is name of your resource file, without .resx extension and without culture name (in your case, name should be Resource (without .en-au).

ResourceManager will try to load specified resource file for current culture, depending on CurrentUICulture. If your server is set to English (Australian) language/regional settings/Culture, it will try to load Resource.en-au if your culture is set to, let's say, Romanian, it will try to find Resource.ro. If such file isn't found, it will fallback to default one, the one without culture name.

Alternatively, you can load resources like this:

[Resources namespace].[Resource file name].ResourceManager.GetString("StringKey")

Resources namespace is Namespace of your resource file (default Resources, but you can see real namespace in your resource file's .Designer.cs file) and resource file name is filename of .resx file, without extension or culture.

So, you can try like this:

string myString = Resources.Resource.ResourceManager.GetString("StringKey");

How to pass a string value to resx file?

You can create new .resx file and add new value using ResXResourceWriter like

 using (ResXResourceWriter resx = new ResXResourceWriter(@".\Resources.resx"))
{
resx.AddResource("StringKey1", "s1");
resx.AddResource("StringKey2", "s2");

}

Here you find more about working with .resx Files Programmatically

If you want get some key from exist resource file you can get it using ResourceManager

 ResourceManager myManager = new ResourceManager(typeof(Resource));
string myString = myManager.GetString("TextBox.Text");
string myString2 = Resource.TextBox_Text;

How to get string resources from specific resource file in C#

I found the working solution.

First of all our resource files are ending like

Resource.resx

and not like Resources.resx

And in second we have resources in a separate assembly: ModelEntities

So the working code is following

var resourceManager = new ResourceManager(typeof(ModelEntities.Properties.Resource)); 
var genderString = resourceManager.GetString("Gender", ci);

How do I get the resource name from the resource object?

I know this is very old, but the accepted answer is no longer necessarily the best answer. As of C# 6.0 you can just use nameof(...):

string resourceName = nameof(Properties.Resources.MyResourceName);
// resourceName == "MyResourceName"

Much simpler!



Related Topics



Leave a reply



Submit