How to get a value from a resource file using its key

How to get a value from a resource file using its key

+8
c #
source share
5 answers

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

+15
source share
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; } 
+8
source share

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

ie: TestProject1.Properties.Resources.MyXmlFile -> direct access to the file in resources

+3
source share

In your .cs file, enter your localization namespace:

  Localization.Resources.KeyName 

Easy and fast :)

-one
source share

Here you can get keyvalue from global resource .

 //TestResource is resource class name. String keyValue=string.Empty; keyValue= Resources.TestResource.KeyString; 
-one
source share

All Articles