Search code examples
c#resource-files

How to read resourcefile from dll c#


I would like to use resourcefiles to get some text.These resourcefiles will be in dll.It's nothing todo with localization just in case you ask. I want to have the ability to choose which rexfile to use based on a configsettings.

Sample MyCompany.RexFiles.dll

  1. RexFileA
  2. RexFileB
  3. RexFileC

My question Given that in a config file I have a settings that decide which rexfile to use eg CurrentRexfile="RexFileB"

How can I default to right rexFile depending on the configSettings.

Any suggestions


Solution

  • You can use the ResourceManager Class to retrieve resources:

    System.Reflection.Assembly myAssembly = this.GetType().Assembly;
    
    string rexFile = ConfigurationManager.AppSettings["CuurentRexfile"];
    System.Reflection.Assembly otherAssembly = System.Reflection.Assembly.Load(rexFile);
    
    System.Resources.ResourceManager resManager = new System.Resources.ResourceManager("ResourceNamespace.myResources", otherAssembly);
    
    string test = resManager.GetString("resourceString");
    

    more read here