Read Value from Resource file using String

2019-02-17 07:38发布

问题:

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