I'm trying to work with a settings.settings f开发者_JS百科ile in my project. There are values that need to be shared between the .exe file and various DLLs. I'd rather not just pass these values around, I'd like to access them when I need them but each project sets up its values with slightly different names and therefore aren't reachable by the other projects.
Is there any way to share the contents of the app.config file between an .exe and a .dll using the settings.settings approach? Or do I need to go back to using ConfigurationManager in order to do this?
Just put your settings in the App.config file, and read them from your dll. In fact I believe it's the only place your dll will look for settings/config, local config for the dll is ignored.
Here's a quick example to ensure the dll has no strong references to the application. This code isn't great but you get the idea.
private string GetSettingValue(string key)
{
string executingAssembly = Assembly.GetEntryAssembly().GetName().Name;
string sectionName = "applicationSettings/" + executingAssembly
+ ".Properties.Settings";
ClientSettingsSection section =
(ClientSettingsSection)ConfigurationManager.GetSection(sectionName);
// add null checking etc
SettingElement setting = section.Settings.Get(key);
return setting.Value.ValueXml.InnerText;
}
Alternatively have a common dll
with the shared settings and take a dependency from each assembly that needs to share the config. This is far cleaner.
精彩评论