如何从.NET资源(RESX)文件中获取字符串值

时间:2013-05-03 14:46:31

标签: c# .net resx

以下是我的RESX文件的样子:

Name            Value        Comments
Rule_seconds    seconds      seconds
Rule_Sound      Sound        Sound

我想要的是: 按字符串命名Value,如下所示:

public string GetResxNameByValue(string value)
{
// some code to get name value
}

并按如下方式实施:

string str = GetResxNameByValue("seconds");

以便str返回Rule_seconds

谢谢!

6 个答案:

答案 0 :(得分:23)

这可行吗

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;

    }

进行一些额外的错误检查..

答案 1 :(得分:5)

您可以通过传递密钥直接访问:

    public  string gtresource(string rulename)
    {
        string value = null;
        System.Resources.ResourceManager RM = new System.Resources.ResourceManager("CodedUITestProject1.Resource1", this.GetType().Assembly);
        value = RM.GetString(rulename).ToString();

        if(value !=null && value !="")
        {
            return value;

        }
        else
        {
            return "";
        }

    }

答案 2 :(得分:2)

以防它可以帮助任何人。此ResourceHelper的灵感来自jureMohan Singh Saini

using System.Collections;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;

public class ResourceHelper
{
    /// <summary>
    ///     ResourceHelper
    /// </summary>
    /// <param name="resourceName">i.e. "Namespace.ResourceFileName"</param>
    /// <param name="assembly">i.e. GetType().Assembly if working on the local assembly</param>
    public ResourceHelper(string resourceName, Assembly assembly)
    {
        ResourceManager = new ResourceManager(resourceName, assembly);
    }

    private ResourceManager ResourceManager { get; set; }

    public string GetResourceName(string value)
    {
        DictionaryEntry entry = ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true).OfType<DictionaryEntry>().FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString() == value);
        return entry.Key.ToString();
    }

    public string GetResourceValue(string name)
    {
        string value = ResourceManager.GetString(name);
        return !string.IsNullOrEmpty(value) ? value : null;
    }
}

答案 3 :(得分:0)

public static class ResourceManagerHelper
{
    public static string GetResourceName(this ResourceManager resourceManager, string value, CultureInfo cultureInfo, bool ignoreCase = false)
    {
        var comparisonType = ignoreCase ? System.StringComparison.OrdinalIgnoreCase : System.StringComparison.Ordinal;
        var entry = resourceManager.GetResourceSet(cultureInfo, true, true)
                                   .OfType<DictionaryEntry>()
                                   .FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString().Equals(value, comparisonType));

        if (entry.Key == null)
            throw new System.Exception("Key and value not found in the resource file");

        return entry.Key.ToString();
    }
}

要调用此扩展方法,

var key = Resources.ResourceManager.GetResourceName(value, CultureInfo.InvariantCulture, true);

在这种情况下,我们不想传递资源程序集,而是可以使用特定资源的resourceManager调用。

答案 4 :(得分:0)

您可以使用此内联代码(c#,ASP.NET MVC)

var _resource = new ResourceManager(typeof(/*your resource*/));
string res = _resource.GetString(/*resource key*/);

我认为这可以帮到你。

答案 5 :(得分:0)

简单明了的方法是-

var keyValue = Resources.ResourceManager.GetString("AboutTitle");

在上面的“ AboutTitle”中是“ KEY”部分,因此,如果您在资源“ AboutTitle =关于”中有上述结果,则 keyValue =关于

我已经在VS2019中的一个项目中使用了它。