简体   繁体   中英

How to get a value from resource file using its key

如何使用其密钥从资源文件中获取值

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;
}

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

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

In .cs file type your localization namespace:

   Localization.Resources.KeyName

Easy and fast :)

You can get keyvalue from global resource here.

//TestResource is resource class name.

String keyValue=string.Empty;

keyValue= Resources.TestResource.KeyString;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM