views:

297

answers:

1

How to read strings from the MyResource.resx file from c#. I am not calling this from the asp.net page, rather i am calling from my bussiness logic.

Assembly assembly = this.GetType().Assembly;
ResourceManager resourceManager = new ResourceManager("MessagesResource", assembly);
resourceManager.GetString("SCHEME_UNQ");

here i am getting exception,

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MessagesResource.resources" was correctly embedded or linked into assembly "App_Web_eerdggo8" at compile time, or that all the satellite assemblies required are loadable and fully signed.

How can i fix this issue????

A: 

Once you add a resx file to your project, Visual Studio will automatically generate a designer class which allows you to read value (In the properties of the resx file you must have Custom Tool: ResXFileCodeGenerator). For example if you add Messages.resx to your project you could directly read values from it:

string value = Messages.SomeResourceKey;
Darin Dimitrov
i not reading the resource file from the aspx page. I am trying to read this from my BussinnessLogic.cs(Bussiness Logic layer).
Nimesh
Is the resource file defined in the Web Tier?
Darin Dimitrov
yeah.. its there in the App_GlobalResources folder. Is this the right way to read..? Is it possible to read the resource file using Resource Mangaer??
Nimesh
In order to read a resource file defined in another assembly you will need to have reference to this assembly, meaning that in your case the business logic tier will need to have a reference to the web tier assembly which is bad. Either place the resources file in the business tier and use the technique I've demonstrated or deport this logic into the web tier.
Darin Dimitrov
ok.. If i move the resource file into the bussiness logic layer...how can i read the file???where can i find the CustomTool:ResxFileCodeGenerator??
Nimesh
In the properties of the resx file, but you probably don't need to modify it as this is the default value. As to the way you read from it, you use the strongly typed class generated by Visual Studio.
Darin Dimitrov