从app.config文件中检索设置的名称

时间:2011-10-24 13:55:28

标签: c# .net c#-4.0 settings

我需要从app.config文件中检索密钥设置的名称。

例如:

我的app.config文件:

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

我知道我可以使用以下方法检索值:

Properties.Settings.Default.IGNORE_CASE

有没有办法从我的密钥设置中获取字符串“IGNORE_CASE”?

2 个答案:

答案 0 :(得分:8)

示例代码here显示如何循环显示所有设置以读取其密钥&amp;值。

为方便起见摘录:

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

答案 1 :(得分:3)

试试这个:

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

while (enumerator.MoveNext())
{
    Debug.WriteLine(((System.Configuration.SettingsProperty)enumerator.Current).Name);
}
使用foreach方法

修改

foreach (System.Configuration.SettingsProperty property in Properties.Settings.Default.Properties)
{
  Debug.WriteLine("{0} - {1}", property.Name, property.DefaultValue);
}
相关问题