本文介绍了使用String从资源文件读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些数据的枚举,还有一个资源文件与枚举的数据相同但使用不同的翻译。Ex。
枚举测试
{
Sun = 1,
Mon = 2
}
资源文件:Text.resx
Sun - >星期天
星期一 - >星期一
现在我想从资源文件获取值,但不使用
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());
我收到以下异常
请尽快帮助
感谢提前
解决方案
资源可能是您的程序集的一部分。为什么不使用以下内容? Resources.ResourceManager.GetString(test.Sun.ToString())
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
please help as soon as you can
Thanks in Advance
解决方案
The resource is probably be a part of your assembly. Why don't you use the following? Resources.ResourceManager.GetString(test.Sun.ToString())
这篇关于使用String从资源文件读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!