Reputation: 553
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
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
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