Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用字符串从资源文件中读取值_C#_Resources_Enums - Fatal编程技术网

C# 使用字符串从资源文件中读取值

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

我有一个枚举,上面有一些数据,还有一个资源文件,数据与枚举相同,但使用不同的转换

Ex.

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”对我有效