简体   繁体   English

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

[英]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. 有一种更简单的方法:Namespace.Properties.Resources.FileName - >获取文件内容的字符串。

ie: TestProject1.Properties.Resources.MyXmlFile -> direct access to the File in the resources 即:TestProject1.Properties.Resources.MyXmlFile - >直接访问资源中的文件

In .cs file type your localization namespace: 在.cs文件中键入您的本地化命名空间:

   Localization.Resources.KeyName

Easy and fast :) 简单快捷:)

You can get keyvalue from global resource here. 您可以在此处从global resource获取keyvalue

//TestResource is resource class name.

String keyValue=string.Empty;

keyValue= Resources.TestResource.KeyString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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