C# 使用字符串从资源文件中读取值
我有一个枚举,上面有一些数据,还有一个资源文件,数据与枚举相同,但使用不同的转换 Ex.C# 使用字符串从资源文件中读取值,c#,resources,enums,C#,Resources,Enums,我有一个枚举,上面有一些数据,还有一个资源文件,数据与枚举相同,但使用不同的转换 Ex. enum test { Sun =1, Mon = 2 } The resource file : Text.resx Sun --> Sunday Mon --> Monday 现在我想从资源文件中获取值,但不使用 string data = Resources.Text.Sun; 但使用我的枚举值,我发现了一些代码,但我有一个例外,代码如下: string reso
enum test
{
Sun =1,
Mon = 2
}
The resource file : Text.resx
Sun --> Sunday
Mon --> Monday
现在我想从资源文件中获取值,但不使用
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());
我得到了以下例外
找不到适合指定区域性的任何资源(或
磁盘上的中性培养基)。基本名称:
~/App\u GlobalResources/Text.resx locationInfo:fileName:
~/App\u GlobalResources/Text.resx.resources
请尽快帮助我
提前感谢如果您使用的是本地资源,且资源项的名称为Sun,您可以编写以下内容
GetLocalResourceObject("Sun").ToString();
已编辑:不要忘记将资源文件命名为您的网页文件名,并使用App\u LocalResources
:
例如:网页是Test.aspx,那么您的资源文件是Test.aspx.resx资源可能是程序集的一部分。你为什么不使用下面的方法呢<代码>Resources.ResourceManager.GetString(test.Sun.ToString())问题不在代码中。系统无法找到*.resx文件以从中获取资源。检查资源文件路径。感谢您提供使用资源的提示。当我的资源文件添加到项目中时,“Resources.ResourceFileNAmeWithoutExtention.KEY_NAMEXXXX”对我有效