locked
Reading a resource file RRS feed

  • Question

  • User-963208184 posted

    Hi All,

    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();
                StreamReader stream;
                
                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.

    Wednesday, May 6, 2015 5:16 AM

Answers

  • User1644755831 posted

    Hello bollcrawley,

    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
    using System.Resources;
    using System.Reflection;
    
    // 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");

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 1:57 AM

All replies

  • User-434868552 posted

    @billcrawley

    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.

    Wednesday, May 6, 2015 11:21 AM
  • User-963208184 posted

    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.

    Wednesday, May 6, 2015 12:31 PM
  • User1644755831 posted

    Hello Billcrawley,

    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.

    With Regards,

    Krunal Parekh

    Thursday, May 7, 2015 4:39 AM
  • User-963208184 posted

    We are going away from my original question of how do I test a resource file.

    Thursday, May 7, 2015 5:44 AM
  • User1644755831 posted

    Hello bollcrawley,

    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
    using System.Resources;
    using System.Reflection;
    
    // 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");

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 1:57 AM