How to get a value from resource file using its key
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
ResourceManager.GetString or ResourceManager.GetStream, depending on the type of the resource.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With