You could group all of your settings into your own custom config section. Then of course you can move it all together to a different file using the configSource
attribute you mention above.
In the case of the AppSettings, your custom config section could merge it's own values into the normal AppSettings (which are NameValueCollection
) using the Add function. This way you shouldn't need to change your client code at all.
As a side, here is some of my base class I use to add "externalConfigSource" attribute to most of my custom elements to allow further file splitting for some of my sub elements (although this is possibly what you are trying to avoid):
public class BaseConfigurationElement : ConfigurationElement
{
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
var fileSource = reader.GetAttribute("externalConfigSource");
if (!String.IsNullOrEmpty(fileSource))
{
var file = new FileInfo(Path.Combine(AppDomainExtensions.ConfigurationFilePath(), fileSource));
if (file.Exists)
{
using (var fileReader = file.OpenRead())
{
var settings = new XmlReaderSettings(){ CloseInput = true, IgnoreProcessingInstructions = true, IgnoreWhitespace = true, IgnoreComments = true};
using (var fileXmlReader = XmlReader.Create(fileReader, settings))
{
var atStart = fileXmlReader.IsStartElement();
base.DeserializeElement(fileXmlReader, serializeCollectionKey);
}
}
reader.Skip();
}
else
{
throw new ConfigurationErrorsException("The file specified in the externalConfigSource attribute cannot be found", reader);
}
}
else
{
base.DeserializeElement(reader, serializeCollectionKey);
}
}
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
if (name == "externalConfigSource")
{
return true; // Indicate that we do know it...
}
return base.OnDeserializeUnrecognizedAttribute(name, value);
}
}
public static class AppDomainExtensions
{
public static string ConfigurationFilePath()
{
return ConfigurationFilePath(AppDomain.CurrentDomain);
}
// http://stackoverflow.com/questions/793657/how-to-find-path-of-active-app-config-file
public static string ConfigurationFilePath(this AppDomain appDomain)
{
return Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
}