Last post May 15, 2015 01:57 AM by Krunal Parekh
May 06, 2015 05:16 AM|billcrawley|LINK
I have a C# VS2013 library project and I have created a resource file via the project properties tab.
How do I access the resource strings that I have created?
I have a solution called PaymentEngine, a project called PaymentEngine and a test project called PaymentEngine.Tests
Under the properties of the PaymentEngine Project in Properties there is a file called Resources.resx and I have set this to public so that the values may be edited outside of the assembly outside of deployment.
I am trying to write a test to ensure the values in the resource file can be read. My test project has a reference to the main project. For now I have:
var assembly = Assembly.GetExecutingAssembly();
stream = new StreamReader(assembly.GetManifestResourceStream("PaymentEngine.Resources.resx"));
but the code errors when trying to set the stream object with Value cannot be null
What it's trying to do is pick up the resource file from the test project which of course doesn't exist.
May 06, 2015 11:21 AM|gerrylowry|LINK
FWIW, resources are generally things that do not change.
Perhaps you have a good reason for wanting to edit your values.
Why not store them in a SQL table or a XML file or even the system registry?
Usually things that i want to change, i put in a SQL table.
if the values have a high security value, simply encrypt them.
May 06, 2015 12:31 PM|billcrawley|LINK
most of the values are Pointers to external web address's or to Css files. The idea is that once deployed, if marketing et al decide to change the css etc, then all they need to do is change the name of the file in the resource file, and similarly for the
URL's as well. I.T.once deployed shouldn't care if these change, and the business should be able to change the values themselves. I could place them in the web config, but this would allow the business access to other values that we don't want to allow them
to change. By keeping them in a resource file we can contain what they can change and what they cant.
May 07, 2015 04:39 AM|Krunal Parekh|LINK
For this you could place them in sql server or an external file which contains the webaddress's url or css files. you could define your own xml structure for this and read the values from xml and then load the urls or css from those. with xml you could just
build several template that would work as themes (if that is what you are trying to do) or configuration settings with have different urls. they would be separate from web.config.
Hope this helps.
May 07, 2015 05:44 AM|billcrawley|LINK
We are going away from my original question of how do I test a resource file.
May 15, 2015 01:57 AM|Krunal Parekh|LINK
The resource file stores items as name-value pair. To get string values from the resource files (.resx) added in your project, use the following code.
//Name Spaces Required
// Create the resource manager.
Assembly assembly = this.GetType().Assembly;
//ResFile.Strings -> <Namespace>.<ResourceFileName i.e. Strings.resx>
resman = new ResourceManager("StringResources.Strings", assembly);
// Load the value of string value for Client
strClientName = resman.GetString("Client");