none
Can't read customized Configuration file RRS feed

  • Question

  • I generated a customized configuration file and try to read it like following.

    ConfigurationManager.OpenExeConfiguration(@"c:\App1.Config");
    var customSections = ConfigurationManager.GetSection(SectionName) as NameValueCollection;
    if (customSections != null)
    {
        foreach (var key in customSections.AllKeys)
        {
            string value = customSections.GetValues(key).FirstOrDefault();
        }
    }

    However customSections is not of App1.Config but of Application's configuration file(App.Config).

    How to read custom sections from customized configuration file?

    Wednesday, September 26, 2018 1:52 AM

Answers

  • Hello Jeff0803,

    1. To open a specific config file, use a combination of the ExeConfigurationFileMap class and the ConfigurationManager.OpenMappedExeConfiguration() method, e.g. :

    ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
    configMap.ExeConfigFilename = @"c:\App1.Config";
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    

    2. After that, it depends on which section of the config file you want to access. E.g. to access the "appSettings" section, you access the "config" object's AppSettings member. This returns an AppSettingsSection object. From there you can access the AppSettingsSection.Settings member and iterate through its items :

    AppSettingsSection appSettingsSection = config.AppSettings;
    KeyValueConfigurationCollection appSettings = appSettingsSection.Settings;
    
    Console.WriteLine("Configuration Settings : ");
    // Display each KeyValueConfigurationElement.
    foreach (KeyValueConfigurationElement keyValueElement in appSettings)
    {
      Console.WriteLine("Key: {0}", keyValueElement.Key);
      Console.WriteLine("Value: {0}", keyValueElement.Value);
    }
    

    3. Hope this helps.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by Jeff0803 Wednesday, September 26, 2018 1:50 PM
    Wednesday, September 26, 2018 6:13 AM
  • Hello Jeff0803,

    1. Creating a custom section in a config file takes more work but it's well worth the effort.

    2. Using the config file presented in your last post as an example, what you need to do is to create a custom ConfigurationSection class. The following is an example for the "MySection1" custom section :

        class MySection1Handler : ConfigurationSection
        {
            // Create a "name" element in the XML config file.
            [ConfigurationProperty("name")]
            public nameElement name
            {
                get
                {
                    return (nameElement)this["name"];
                }
                set
                { 
                    this["name"] = value; 
                }
            }
    
            // Create the class that will handle the value setting and getting
            // for the "name" element.
            public class nameElement : ConfigurationElement
            {
                [ConfigurationProperty("Value", DefaultValue = "", IsRequired = true)]
                public String Value
                {
                    get
                    {
                        return (String)this["Value"];
                    }
                    set
                    {
                        this["Value"] = value;
                    }
                }
            }
    
            // Create a "phone" element in the XML config file.
            [ConfigurationProperty("phone")]
            public phoneElement phone
            {
                get
                {
                    return (phoneElement)this["phone"];
                }
                set
                {
                    this["phone"] = value;
                }
            }
    
            // Create the class that will handle the value setting and getting
            // for the "phone" element.
            public class phoneElement : ConfigurationElement
            {
                [ConfigurationProperty("Value", DefaultValue = "", IsRequired = true)]
                public String Value
                {
                    get
                    {
                        return (String)this["Value"];
                    }
                    set
                    {
                        this["Value"] = value;
                    }
                }
            }
        }

    3. Then in your code for reading the config settings, use the Configuration.GetSection() method to obtain a ConfigurationSection object. After that, cast the returned ConfigurationSection object to the MySection1Handler class, e.g. :

            static void ReadConfigCustomSection()
            {
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
                configMap.ExeConfigFilename = @"c:\App1.Config";
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
                ConfigurationSection custom_section = config.GetSection("MySection1");
                MySection1Handler mySection1Handler =
                    (MySection1Handler)custom_section;
    
                Console.WriteLine("name  : {0:S}", mySection1Handler.name.Value);
                Console.WriteLine("phone : {0:S}", mySection1Handler.phone.Value);
            }
    

    3.1 After that, access the values of the "name" and "phone" elements via the name and phone configuration properties as shown above.

    4. Your config file will also need to include the type for handling the MySection1 section, you do this by adding a "type" attribute in the section element for "MySection1". The following is an example :

    <?xml version="1.0"?>
    <configuration>
        <configSections>
            <section name="MySection1" type="ConsoleTestApp.MySection1Handler, ConsoleTestApp" />
        </configSections>
        <MySection1>
            <name Value="aaaa" />
            <phone Value="111-222-3333" />
        </MySection1>
    </configuration>
    

    4.1 Note that I have defined the MySection1Handler class inside the assembly ConsoleTestApp. Hence the type indicates the class "ConsoleTestApp.MySection1Handler" inside the assembly "ConsoleTestApp".

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by Jeff0803 Friday, September 28, 2018 2:47 AM
    Wednesday, September 26, 2018 3:20 PM

All replies

  • Hello Jeff0803,

    1. To open a specific config file, use a combination of the ExeConfigurationFileMap class and the ConfigurationManager.OpenMappedExeConfiguration() method, e.g. :

    ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
    configMap.ExeConfigFilename = @"c:\App1.Config";
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    

    2. After that, it depends on which section of the config file you want to access. E.g. to access the "appSettings" section, you access the "config" object's AppSettings member. This returns an AppSettingsSection object. From there you can access the AppSettingsSection.Settings member and iterate through its items :

    AppSettingsSection appSettingsSection = config.AppSettings;
    KeyValueConfigurationCollection appSettings = appSettingsSection.Settings;
    
    Console.WriteLine("Configuration Settings : ");
    // Display each KeyValueConfigurationElement.
    foreach (KeyValueConfigurationElement keyValueElement in appSettings)
    {
      Console.WriteLine("Key: {0}", keyValueElement.Key);
      Console.WriteLine("Value: {0}", keyValueElement.Value);
    }
    

    3. Hope this helps.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by Jeff0803 Wednesday, September 26, 2018 1:50 PM
    Wednesday, September 26, 2018 6:13 AM
  • How to read custom section name and custom section body?

    for example,

    <?xml version="1.0"?>
    <configuration>
        <configSections>
            <section name="MySection1" .....>
        </configSections>
        <MySection1>
            <add key="name" value="aaaa" />
            <add key="phone" value="111-222-3333" />
        </MySection1>
    </configuration>
    
    How to read "MySection1" in <configSections> </configSections>?

    Wednesday, September 26, 2018 2:24 PM
  • Hello Jeff0803,

    1. Creating a custom section in a config file takes more work but it's well worth the effort.

    2. Using the config file presented in your last post as an example, what you need to do is to create a custom ConfigurationSection class. The following is an example for the "MySection1" custom section :

        class MySection1Handler : ConfigurationSection
        {
            // Create a "name" element in the XML config file.
            [ConfigurationProperty("name")]
            public nameElement name
            {
                get
                {
                    return (nameElement)this["name"];
                }
                set
                { 
                    this["name"] = value; 
                }
            }
    
            // Create the class that will handle the value setting and getting
            // for the "name" element.
            public class nameElement : ConfigurationElement
            {
                [ConfigurationProperty("Value", DefaultValue = "", IsRequired = true)]
                public String Value
                {
                    get
                    {
                        return (String)this["Value"];
                    }
                    set
                    {
                        this["Value"] = value;
                    }
                }
            }
    
            // Create a "phone" element in the XML config file.
            [ConfigurationProperty("phone")]
            public phoneElement phone
            {
                get
                {
                    return (phoneElement)this["phone"];
                }
                set
                {
                    this["phone"] = value;
                }
            }
    
            // Create the class that will handle the value setting and getting
            // for the "phone" element.
            public class phoneElement : ConfigurationElement
            {
                [ConfigurationProperty("Value", DefaultValue = "", IsRequired = true)]
                public String Value
                {
                    get
                    {
                        return (String)this["Value"];
                    }
                    set
                    {
                        this["Value"] = value;
                    }
                }
            }
        }

    3. Then in your code for reading the config settings, use the Configuration.GetSection() method to obtain a ConfigurationSection object. After that, cast the returned ConfigurationSection object to the MySection1Handler class, e.g. :

            static void ReadConfigCustomSection()
            {
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
                configMap.ExeConfigFilename = @"c:\App1.Config";
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
                ConfigurationSection custom_section = config.GetSection("MySection1");
                MySection1Handler mySection1Handler =
                    (MySection1Handler)custom_section;
    
                Console.WriteLine("name  : {0:S}", mySection1Handler.name.Value);
                Console.WriteLine("phone : {0:S}", mySection1Handler.phone.Value);
            }
    

    3.1 After that, access the values of the "name" and "phone" elements via the name and phone configuration properties as shown above.

    4. Your config file will also need to include the type for handling the MySection1 section, you do this by adding a "type" attribute in the section element for "MySection1". The following is an example :

    <?xml version="1.0"?>
    <configuration>
        <configSections>
            <section name="MySection1" type="ConsoleTestApp.MySection1Handler, ConsoleTestApp" />
        </configSections>
        <MySection1>
            <name Value="aaaa" />
            <phone Value="111-222-3333" />
        </MySection1>
    </configuration>
    

    4.1 Note that I have defined the MySection1Handler class inside the assembly ConsoleTestApp. Hence the type indicates the class "ConsoleTestApp.MySection1Handler" inside the assembly "ConsoleTestApp".

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by Jeff0803 Friday, September 28, 2018 2:47 AM
    Wednesday, September 26, 2018 3:20 PM
  • Thanks it works fine!

    I need add more features but can start from this.

    Thanks again.

    • Marked as answer by Jeff0803 Friday, September 28, 2018 2:47 AM
    • Unmarked as answer by Jeff0803 Friday, September 28, 2018 2:47 AM
    Friday, September 28, 2018 2:47 AM