在 ResXResourceReader 中从项目 C# 中读取资源文件
Read resource file from project C# in ResXResourceReader
我在加载资源文件时遇到问题。我的代码如下所示:
var reader = new ResXResourceReader(@"C:\Users\work\Projects\WebApps\source\MPS\App_GlobalResources\conf\strings.resx")
上面的代码在我的机器上工作得很好,但这只是开发,当我将应用程序部署到服务器时,我需要动态加载项目路径。我遇到的问题 我无法使用 @".\App_GlobalResources\conf\strings.resx"
或 @"App_GlobalResources\conf\strings.resx"
App_GlobalResources 是该项目根路径中的文件夹。我尝试的一切都不起作用。有什么建议吗?
由于您使用的是 ASP.NET Web 表单,因此不需要通过 ResXResourceReader
加载资源文件。相反,您可以通过多种方式引用资源文件。
首先,对于本地资源(在App_LocalResources
文件夹中),您可以通过以下方式引用:
C# 代码隐藏:
var resourceValue = this.GetLocalResourceObject("ResourceKey").ToString();
ASPX 页面:
<asp:Label ID="MyResource" runat="server" Text="<%$Resources:, ResourceKey %>" />
对于全局资源(在App_GlobalResources
),可以参考如下。在下面的示例中,您不会在文件名中包含 .resx
扩展名,因此如果您有一个名为 ResourceStrings.resx
的全局资源文件,您可以将其引用为 ResourceStrings
:
C# 代码隐藏:
var resourceValue = this.GetGlobalResourceObject("ResourceFile", "ResourceKey").ToString();
ASPX 页面:
<asp:Label ID="MyResource" runat="server" Text="<%$Resources:ResourceFile, ResourceKey %>" />
如果你想动态加载文件,那么看Server.MapPath
,你可以这样调用:
Server.MapPath(@".\App_GlobalResources\conf\strings.resx");
我在加载资源文件时遇到问题。我的代码如下所示:
var reader = new ResXResourceReader(@"C:\Users\work\Projects\WebApps\source\MPS\App_GlobalResources\conf\strings.resx")
上面的代码在我的机器上工作得很好,但这只是开发,当我将应用程序部署到服务器时,我需要动态加载项目路径。我遇到的问题 我无法使用 @".\App_GlobalResources\conf\strings.resx"
或 @"App_GlobalResources\conf\strings.resx"
App_GlobalResources 是该项目根路径中的文件夹。我尝试的一切都不起作用。有什么建议吗?
由于您使用的是 ASP.NET Web 表单,因此不需要通过 ResXResourceReader
加载资源文件。相反,您可以通过多种方式引用资源文件。
首先,对于本地资源(在App_LocalResources
文件夹中),您可以通过以下方式引用:
C# 代码隐藏:
var resourceValue = this.GetLocalResourceObject("ResourceKey").ToString();
ASPX 页面:
<asp:Label ID="MyResource" runat="server" Text="<%$Resources:, ResourceKey %>" />
对于全局资源(在App_GlobalResources
),可以参考如下。在下面的示例中,您不会在文件名中包含 .resx
扩展名,因此如果您有一个名为 ResourceStrings.resx
的全局资源文件,您可以将其引用为 ResourceStrings
:
C# 代码隐藏:
var resourceValue = this.GetGlobalResourceObject("ResourceFile", "ResourceKey").ToString();
ASPX 页面:
<asp:Label ID="MyResource" runat="server" Text="<%$Resources:ResourceFile, ResourceKey %>" />
如果你想动态加载文件,那么看Server.MapPath
,你可以这样调用:
Server.MapPath(@".\App_GlobalResources\conf\strings.resx");