0

I have a c# library with a setting file. I would like to know if there is a way to publish the settings in a external file, in order to the users can edit settings and change some values.

I have been trying several combinations but always the settings file is embedded its value to dll compiled file, I would like to have something like app.config but with libraries. Is this possible?

Thanks

Marc Cals
  • 2,963
  • 4
  • 30
  • 48
  • possible duplicate of [Create an app.config for an Assembly](http://stackoverflow.com/questions/1140655/create-an-app-config-for-an-assembly) – Alex K. Dec 10 '14 at 15:17

2 Answers2

0

Yes, it is. You only need to load it dynamically in code and use like normal app config:

var path = @"C:\YourLib.dll"; // your config file should be named YourLib.dll.config
Configuration Config = ConfigurationManager.OpenExeConfiguration(path); // this may be class field

Setting1 = Config.AppSettings.Settings["Setting1"] != null ? Config.AppSettings.Settings["Setting1"].Value : something; 

I'm not sure if relative path works, feel free to experiment :)

Krzysztof Skowronek
  • 2,796
  • 1
  • 13
  • 29
0

I think that I was trying to solve it in a bad approach, as explained in https://stackoverflow.com/a/7263047/1081568 configuration must be in the executable application and not in the library class.

To solve it I have created a class in my library of properties Settings, that reads the configuration of my web/app.config with System.Configuration.ConfigurationManager.AppSettings["NameOfSetting"]

Community
  • 1
  • 1
Marc Cals
  • 2,963
  • 4
  • 30
  • 48