Duncan_McCloud
Duncan_McCloud

Reputation: 553

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 ?

Upvotes: 4

Views: 3618

Answers (2)

Davide Piras
Davide Piras

Reputation: 44605

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);
}

Upvotes: 3

Robert Levy
Robert Levy

Reputation: 29073

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]);
    }
}

Upvotes: 8

Related Questions