从app.config文件中检索设置的名称
本文关键字:设置 检索 app config 文件 | 更新日期: 2023-09-27 18:12:07
我需要从app.config文件中检索密钥设置的名称。
例如:我的app.config文件:
<setting name="IGNORE_CASE" serializeAs="String">
<value>False</value>
</setting>
我知道我可以检索值使用:
Properties.Settings.Default.IGNORE_CASE
是否有办法从我的键设置中获得字符串"IGNORE_CASE"?
下面的示例代码展示了如何遍历所有设置以读取它们的键&价值。
方便节选:
// 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]);
}
}
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);
}