从app.config文件中检索设置的名称
我需要从app.config文件中检索密钥设置的名称。
例如:
我的app.config文件:
False
我知道我可以使用以下方法检索值:
Properties.Settings.Default.IGNORE_CASE
有没有办法从我的密钥设置中获取字符串“IGNORE_CASE”?
试试这个:
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); }
此处的示例代码显示了如何遍历所有设置以读取其键和值。
摘录方便:
// 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]); } }