简体   繁体   中英

Read Value from Resource file using String

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM