简体   繁体   English

在 Properties.Settings.Default 中迭代属性

[英]Iterating properties in Properties.Settings.Default

I've got a Visual Studio project that contains a few StringCollections under app settings.我有一个 Visual Studio 项目,其中包含应用程序设置下的一些 StringCollections。 I want to iterate over these properties/settings at runtime but can't find a way to do so.我想在运行时迭代这些属性/设置,但找不到这样做的方法。 I've tried this:我试过这个:

foreach (SettingsProperty prop in Properties.Settings.Default.Properties)
   string className = (prop.DefaultValue as StringCollection)[1];

But prop.DefaultValue is not a StringCollection and returns null.但是prop.DefaultValue不是StringCollection并返回 null。 I realize I can use Properties.Settings.Default["SettingName"] but that requires me to know the setting name beforehand.我意识到我可以使用Properties.Settings.Default["SettingName"]但这需要我事先知道设置名称。 There has to be a way to iterate through the application setting names and read their associated StringCollection values, but how?必须有一种方法来遍历应用程序设置名称并读取它们关联的StringCollection值,但是如何? I can't find an example after hours of searching.经过数小时的搜索,我找不到示例。

How about using怎么用

if(Properties.Settings.Default[prop.Name] is StringCollection sc){
    ...
}

in your loop?在你的循环中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM