c# - Equivalent to 'app.config' for a library (DLL) -


is there equivalent app.config libraries (dlls)? if not, easiest way store configuration settings specific library? please consider library might used in different applications.

you can have separate configuration file, you'll have read "manually", configurationmanager.appsettings["key"] read config of running assembly.

assuming you're using visual studio ide, can right click desired project → add → new item → application configuration file

this add app.config project folder, put settings in there under <appsettings> section.

now read file have such function:

string getappsetting(configuration config, string key) {     keyvalueconfigurationelement element = config.appsettings.settings[key];     if (element != null)     {         string value = element.value;         if (!string.isnullorempty(value))             return value;     }     return string.empty; } 

and use it:

configuration config = null; string execonfigpath = this.gettype().assembly.location; try {     config = configurationmanager.openexeconfiguration(execonfigpath); } catch (exception ex) {     //handle errror here.. means dll has no sattelite configuration file. }  if (config != null) {     string myvalue = getappsetting(config, "mykey");     ... } 

you'll have add reference system.configuration namespace in order have configurationmanager class available.

when building project, in addition dll you'll have dllname.dll.config file well, that's file have publish dll itself.

the above basic sample code, interested in full scale example, please refer this other answer.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -