Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve name of the Setting from app.config file

I need to retrieve the name of the key setting from app.config file.

For instance:

My app.config file:

<setting name="IGNORE_CASE" serializeAs="String">
    <value>False</value>
</setting>

I know i can retrieve the value using:

Properties.Settings.Default.IGNORE_CASE

Is there a way to get the string "IGNORE_CASE" from my key setting ?

like image 362
Duncan_McCloud Avatar asked Dec 22 '22 07:12

Duncan_McCloud


2 Answers

The sample code here shows how to loop over all settings to read their key & value.

Excerpt for convenience:

// Get the AppSettings section.        
// This function uses the AppSettings property
// to read the appSettings configuration 
// section.
public static void ReadAppSettings()
{
    // Get the AppSettings section.
    NameValueCollection appSettings =
       ConfigurationManager.AppSettings;

    // Get the AppSettings section elements.
    for (int i = 0; i < appSettings.Count; i++)
    {
      Console.WriteLine("#{0} Key: {1} Value: {2}",
        i, appSettings.GetKey(i), appSettings[i]);
    }
}
like image 88
Robert Levy Avatar answered Jan 10 '23 19:01

Robert Levy


try this:

System.Collections.IEnumerator enumerator = Properties.Settings.Default.Properties.GetEnumerator();

while (enumerator.MoveNext())
{
    Debug.WriteLine(((System.Configuration.SettingsProperty)enumerator.Current).Name);
}

Edit: with foreach approach as suggested

foreach (System.Configuration.SettingsProperty property in Properties.Settings.Default.Properties)
{
  Debug.WriteLine("{0} - {1}", property.Name, property.DefaultValue);
}
like image 38
Davide Piras Avatar answered Jan 10 '23 19:01

Davide Piras