简体   繁体   English

使用String从资源文件读取值

[英]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 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 如果您使用的是本地资源,并且资源项的名称是Sun,则可以编写以下内容

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 编辑:不要忘记将资源文件命名为ur网页文件名并使用App_LocalResources :EX:网页是Test.aspx ,那么你的资源文件是Test.aspx.resx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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