Is there an equivalent to app.config
for libraries (DLLs)? If not, what is the easiest way to store configuration settings that are specific to a library? Please consider that the library might be used in different applications.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Preamble: I'm using NET 2.0;
The solution posted by Yiannis Leoussis is acceptable but I had some problem with it.
First, the
static AppSettingsSection AppSettings = (AppSettingsSection)myDllConfig.GetSection("appSettings");
returns null. I had to change it tostatic AppSettingSection = myDllConfig.AppSettings;
Then the
return (T)Convert.ChangeType(AppSettings.Settings[name].Value, typeof(T), nfi);
doesn't have a catch for Exceptions. So I've changed itThis works very well but if you have a different dll you have to rewrite every time the code for every assembly. So, this is my version for an Class to instantiate every time you need.
For a config:
Use it as:
As far as I'm aware, you have to copy + paste the sections you want from the library .config into the applications .config file. You only get 1 app.config per executable instance.
Just for something to do, I refactored the top answer into a class. The usage is something like:
If you add Settings to a Class Library project in Visual Studio (Project Properties, Settings), it will add an app.config file to your project with the relevant userSettings/applicatioNSettings sections, and the default values for these settings from your Settings.settings file.
However this configuration file will not be used at runtime - instead the class library uses the configuration file of its hosting application.
I believe the main reason for generating this file is so that you can copy/paste the settings into the host application's configuration file.
I am currently creating plugins for a retail software brand, which are actually .net class libraries. As a requirement, each plugin needs to be configured using a config file. After a bit of research and testing, I compiled the following class. It does the job flawlessly. Note that I haven't implemented local exception handling in my case because, I catch exceptions at a higher level.
Some tweaking maybe needed to get the decimal point right, in case of decimals and doubles, but it works fine for my CultureInfo...
App.Config file sample
Usage:
Credits to Shadow Wizard & MattC
Use add existing item, select the app config from dll project. Before clicking add, use the little down arrow on the right hand side of the add button to "add as link"
I do this all the time in my dev.