zuboje
zuboje

Reputation: 726

Read resource file from project C# in ResXResourceReader

I have issue loading resource file. My code looks like this:

var reader = new ResXResourceReader(@"C:\Users\work\Projects\WebApps\source\MPS\App_GlobalResources\conf\strings.resx")

Code above works perfectly fine on my machine, but that is just development, when I deploy application to a server I need to load project path dynamically. Problem that I am experiencing I cannot use @".\App_GlobalResources\conf\strings.resx" nor @"App_GlobalResources\conf\strings.resx"

App_GlobalResources is folder that is in root path of this project. Everything I tried does not work. Any suggestions?

Upvotes: 0

Views: 1672

Answers (1)

entropic
entropic

Reputation: 1683

Since you're using ASP.NET Web Forms, you don't need to load the resource file via a ResXResourceReader. Instead, you can reference the resource file in a number of ways.

First, for local resources (in App_LocalResources folders), you can reference it in the following ways:

C# Code Behind:

var resourceValue = this.GetLocalResourceObject("ResourceKey").ToString();

ASPX Page:

<asp:Label ID="MyResource" runat="server" Text="<%$Resources:, ResourceKey %>" />

For global resources (in App_GlobalResources), you can reference it as follows. In the below example, you wouldn't include the .resx extension in the file name, so if you had a global resource file named ResourceStrings.resx, you would reference it as ResourceStrings:

C# Code Behind:

var resourceValue = this.GetGlobalResourceObject("ResourceFile", "ResourceKey").ToString();

ASPX Page:

<asp:Label ID="MyResource" runat="server" Text="<%$Resources:ResourceFile, ResourceKey %>" />

If you want to load the file dynamically, then look at Server.MapPath, which you would be able to call as follows:

Server.MapPath(@".\App_GlobalResources\conf\strings.resx");

Upvotes: 1

Related Questions