Visual Studio 2005窗体配置文件
http://tech.ddvip.com 2008年01月22日 社区交流
内容摘要:本文聚焦于简单的桌面应用程序,该应用程序的唯一目的是显示和修改自己的配置文件的内容。如果要运行这个应用程序,你必须下载Visual Studio 2005公众beta版。
枚举配置属性
ThisConfigEditor的显示的核心是ThisConfigEditor.cs代码文件中的私有的PopulateListView方法:
private void PopulateListView()
{
ListViewItem item = null;
this.buttonUpdateSetting.Enabled = false;
this.textBoxSettingValue.Enabled = false;
this.listViewSettings.Items.Clear();
Properties.Settings settings = Properties.Settings.Default;
foreach (SettingsProperty property in settings.Properties)
{
bool match = false;
switch (_dt)
{
case DisplayType.All:
match = true;
break;
case DisplayType.Application:
foreach (System.Collections.DictionaryEntry attribute in property.Attributes)
{
if (attribute.Value is System.Configuration.ApplicationScopedSettingAttribute)
{
match = true;
break;
}
}
break;
case DisplayType.User:
foreach (System.Collections.DictionaryEntry attribute in property.Attributes)
{
if (attribute.Value is System.Configuration.UserScopedSettingAttribute)
{
match = true;
break;
}
}
break;
}
if (match)
{
item = new ListViewItem(property.Name);
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, property.PropertyType.ToString()));
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, settings[item.Text] as string));
item.Tag = property;
this.listViewSettings.Items.Add(item);
}
}
}
通过判断私有枚举字段_dt(它与窗体中的组合框的选择相关联,该组合框用于选择显示哪些配置属性:全部的、应用程序的或者用户的),代码枚举出配置属性。集合中的每个成员都是一个SettingProperty实例(System.Configuration API中的另外一个新类,用于表现独立的配置属性),它也包含一个DictionaryEntry实例集合,用于表示它们的多个属性。通过在属性集合上进行迭代操作,代码搜索出与组合框中选择的范围相匹配的属性。在匹配到之后,代码显示通过建立一个新的ListViewItem并把它添加到窗体的ListView上,从而显示配置属性。
来源:天极 作者:陶刚 责编:豆豆技术应用