应用设置架构
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
好吧,一个老帖子,但是当我遇到类似情况时我记得它:
...
如果您转到项目/项目属性(在 VS2008 或 VS2010 中)。
有一个“设置”选项卡。
如果你添加一个新值......
其中一种类型被称为:
System.Collections.Specialized.StringCollection
给它起个名字(我用的是“FavoriteColors”)。
设置类型(如上所示)。
设置值。
“字符串集合编辑器”显示“在集合中输入字符串(每行一个)”。
我输入了:
红色
黄色
黑色
白色
这会将一些 xml 添加到您的 app.config 文件中。
<setting name="FavoriteColors" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>red</string>
<string>yellow</string>
<string>black</string>
<string>white</string>
</ArrayOfString>
</value>
</setting>
(您最好完成这些步骤,而不是粘贴上面的 xml,因为(为简洁起见)我没有将所有 xml 添加到生成的这篇文章中。
您应该能够通过如下代码“获取”这些值:
private void ShowMyFavoriteColors()
{
Properties.Settings.Default.FavoriteColors.Cast<string>().ToList().ForEach(myfavcolor =>
{
string temp = myfavcolor;
});
}
请注意,上述步骤将生成以下 C# 代码(为您创建的自动代码......这是您创建的不是代码)
但代码如下所示:
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(@"<?xml version=""1.0"" encoding=""utf-16""?>
<ArrayOfString xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<string>red</string>
<string>yellow</string>
<string>black</string>
<string>white</string>
</ArrayOfString>")]
public global::System.Collections.Specialized.StringCollection FavoriteColors {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["FavoriteColors"]));
}
}
}
}