Retrieve name of the Setting from app.config file

谁说我不能喝 提交于 2019-12-07 11:41:35

问题


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 ?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/7876733/retrieve-name-of-the-setting-from-app-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!