Search code examples
asp.netresxclass-library

Read a specific resx file within a ClassLibrary


I have a ClassLibrary with a C# Class and a folder with resx files.

I have to read a specific Resx file by culture within the C# class.

I tried the following code (both GetLocalResourceObject and GetGlobalResourceObject), but I get this error: 'The relative virtual path is not allowed here'.

HttpContext.GetLocalResourceObject("Folder.ResxFileName", "ResxKey", new CultureInfo("it-IT"))

How do I have to set the Resx file path to get it working?

(The ClassLibrary dll is referenced in a website project)


Solution

  • You've just to use the ResourceManager exposed by the class library (with the namespace 'ClassWithResx'):

    ClassWithResx.Resources.ResourceManager.GetString("ResxKey", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
    

    this works supposing you've a Resources.resx and Resources.en-US.resx (or whatelse) file in the root of your class library, and you've to mark with the custom tool the access modifier of the resource strings to "Public".