从资源文件中动态获取值

本文关键字:获取 动态 资源 源文件 | 更新日期: 2023-09-27 18:24:35

在我的解决方案中,我有一个文件夹App_GlobalResources。在我的文件夹App_GlobalResources中,我有两个文件Lang.fr.resx和Lang.resx.

在运行时,我想决定应该加载哪个文件,并从该文件中获取文本

当我写下面的代码时,它显示我为:

The resource class for this page was not found.  Please check if the resource file exists and try again.
colName = GetLocalResourceObject( "ColumnNamePrefix_" + colName.Substring(0, colName.Length - 3) ).ToString();

有人能告诉我该怎么办吗?

从资源文件中动态获取值

如果它在App_GlobalResources中,则要使用GetGlobalResourceObject

有一个重载需要CultureInfo,这就是您可以指定默认版本(我假设是英语版本?)或法语版本的方式。应该是这样的:

CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
HttpContext.GetGlobalResourceObject("ColumnNamePrefix_" + colName.Substring(0, colName.Length - 3),
    "Lang", culture);