How to Load Config File Programmatically

前端 未结 4 1626
[愿得一人]
[愿得一人] 2020-12-01 06:42

Suppose I have a Custom Config File which corresponds to a Custom-defined ConfigurationSection and Config elements. These config classes are stored in a library.

C

4条回答
  •  感动是毒
    2020-12-01 07:34

    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.

提交回复
热议问题