使用字符串从资源文件中读取值
我有一个带有一些数据的枚举,还有一个资源文件,其中包含与枚举相同的数据,但使用不同的翻译
Ex。
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
请尽快提供帮助
提前致谢
I have an enum with some data on it, also I have a resource file with the same data of the enum but using different translation
Ex.
enum test
{
Sun =1,
Mon = 2
}
The resource file : Text.resx
Sun --> Sunday
Mon --> Monday
Now I want to get the value from the resource file but not using
string data = Resources.Text.Sun;
but using my enum value , I have found some code but I have an exception, the code is following :
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());
and I got the following Exception
Could not find any resources appropriate for the specified culture (or
the neutral culture) on disk. baseName:
~/App_GlobalResources/Text.resx locationInfo: fileName:
~/App_GlobalResources/Text.resx.resources
please help as soon as you can
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该资源可能是您程序集的一部分。为什么不使用以下内容呢?
Resources.ResourceManager.GetString(test.Sun.ToString())
The resource is probably be a part of your assembly. Why don't you use the following?
Resources.ResourceManager.GetString(test.Sun.ToString())
如果您使用的是本地资源并且资源项的名称是Sun,您可以编写以下内容
编辑:不要忘记将资源文件命名为您的网页文件名并使用
App_LocalResources
:例如:网页是 Test.aspx ,那么您的资源文件是 Test.aspx.resx
If you are using a local resource and the name of the resource item is Sun, u can write the following
Edited: don't forget to name the resource file as ur webpage file name and use a
App_LocalResources
:EX: webpage is Test.aspx , then your resource file is Test.aspx.resx