Read Value from Resource file using String

末鹿安然 提交于 2019-12-30 08:41:51

问题


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


回答1:


The resource is probably be a part of your assembly. Why don't you use the following? Resources.ResourceManager.GetString(test.Sun.ToString())




回答2:


If you are using a local resource and the name of the resource item is Sun, u can write the following

GetLocalResourceObject("Sun").ToString();

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



来源:https://stackoverflow.com/questions/7644796/read-value-from-resource-file-using-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!