使用字符串从资源文件中读取值

6
我有一个枚举类型,其中包含一些数据,同时我还有一个资源文件,其中包含相同的枚举数据,但使用不同的翻译。
enum test
{
    Sun =1,
    Mon = 2
}

The resource file : Text.resx

Sun --> Sunday
Mon --> Monday

现在我想从资源文件中获取值,但不想使用。
string data = Resources.Text.Sun; 

但是使用我的枚举值,我找到了一些代码,但出现了异常,代码如下:
string resourceFile = "~/App_GlobalResources/Text.resx";
string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
string resourceValue = resourceManager.GetString(myEnum.ToString());

我得到了以下异常:
找不到适合指定区域性(或中性区域性)的任何资源。基本名称: ~/App_GlobalResources/Text.resx位置信息:文件名: ~/App_GlobalResources/Text.resx.resources
请尽快提供帮助。
提前致谢。

问题不在你的代码上。系统找不到*.resx文件以从中获取资源。请检查资源文件路径。 - Samich
2个回答

6
这个资源可能是你的程序集的一部分。为什么不使用以下代码呢?Resources.ResourceManager.GetString(test.Sun.ToString())

感谢提示使用资源。当我的资源文件添加到项目中时,'Resources.ResourceFileNAmeWithoutExtention.KEY_NAMEXXXX' 对我有用。 - Ram

-1
如果您正在使用本地资源,且资源项的名称为Sun,则可以编写以下内容:
GetLocalResourceObject("Sun").ToString();

编辑:不要忘记将资源文件命名为您的网页文件名,并使用 App_LocalResources

例如:网页是 Test.aspx,那么您的资源文件就是 Test.aspx.resx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接