How to Load Config File Programmatically

You’ll have to adapt it for your requirements, but here’s the code I use in one of my projects to do just that:

var fileMap = new ConfigurationFileMap("pathtoconfigfile");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs
var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyTarget.Namespace.Properties.Settings"); // This is the section name, change to your needs
var setting = section.Settings.Get("SettingName"); // This is the setting name, change to your needs
return setting.Value.ValueXml.InnerText;

Note that I’m reading a valid .net config file. I’m using this code to read the config file of an EXE from a DLL. I’m not sure if this works with the example config file you gave in your question, but it should be a good start.

Leave a Comment