Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access app configuration from a .dll?

I recently broke out a part of my winform app in a .dll. Some of the classes in that dll wants fetch/store user settings. The classes just used the VS generated Settings file so it just did Properties.Settings.Default.SomeSetting = var;Properties.Settings.Default.Save() etc.

What are my options now that I moved that code out to a class library/.dll ?

like image 647
leeeroy Avatar asked Feb 22 '10 19:02

leeeroy


People also ask

Can a DLL have app config file?

Unfortunately, you can only have one app. config file per executable, so if you have DLL's linked into your application, they cannot have their own app. config files.

Where are app config files stored?

The file is stored inside this path "bin/debug/app. config", if you make changes while debugging, those changes should appear there. Just remember that this file is overwritten with the "app. config" from the project root each time you run the application on Visual Studio IDE.

How do I access config in C#?

New Project > Visual C# > Console Application Configuration assembly reference to access configuration settings, using ConfigurationManager. To add the reference, just right click References and click to add references. Now, we can see that System. Configuration reference has been added successfully to our project.

Can class library have app config?

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file.


2 Answers

The hosting application should handle the interface to the config file, not the DLL. Either

  1. Pass whatever settings need to be read/modified within the DLL as parameters, or

  2. Pass in a name-value collection of settings that can be modified by the DLL, and save whatever changes are made by the DLL to the collection when control returns to the calling application.

This is similar in principle to removing a database interface from the business layer of a tiered application and encapsulating it into a data layer.

like image 53
3Dave Avatar answered Oct 19 '22 23:10

3Dave


It doesn't make a lot of sense to me to have a DLL storing user settings. A DLL is a library, not an application, and doesn't directly interact with the user. If classes in the DLL need access to user settings, you can pass them in as parameters.

like image 37
Instance Hunter Avatar answered Oct 20 '22 01:10

Instance Hunter