Shaul Behr
Shaul Behr

Reputation: 38101

How to preserve app settings from a referenced assembly?

I have an app MainApp that references another project MyDLL.dll. Inside the MyDLL project I have made some user settings in a Settings.settings file that may be changed at runtime. So it appears that these settings get saved in the app.config file of MyDLL. But the problem is, the main project is MainApp, and MyDLL.dll.config does not, so far as I can see, get copied to the MainApp output folder. This is reflected in the fact that even though I save the settings in the code of MyDLL, the next time I run MainApp the settings have gone back to the default.

I must be missing something really obvious here. There has to be a way for related assemblies to preserve their settings values. But how?

Upvotes: 1

Views: 402

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40160

While you can add an app.config to a library project, it has no effect to do so. Configuration is linked to the application, not the library.

You need to create the settings and configuration in your application itself. You can do something like including the library's app.config if you really wanted to, but that would probably not do what you want, either. It's best to just handle your configuration in the application.

Why is this so? Because what's to say it's valid to have user settings for your library in the first place? A library should not be tied to any particular kind of application. What if you used it in a Windows Service or an ASP.NET application?

Upvotes: 3

Related Questions