Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Working with Windows Forms Configuration Files in .NET 2.0 and Visual Studio 2005 : Page 3

The newest version of the .NET framework extends strong data typing to configuration files, adds scope for settings, a GUI-based property editor, and drops the read-only restriction for configuration classes.


advertisement
What Gets Generated?
All this work in the Properties Editor generates configuration settings and a great deal of code automatically. While it is beyond the scope of this article to dig into the content in detail, it is useful to take a look. If you open the app.config file in the downloadable code that accompanies this article, you can see what Visual Studio creates as the ThisConfigEditor.exe.config file when you build the project. Even more interesting is the Settings.Designer.cs source (available in the downloadable code). This class provides easy and direct access to each setting by name in a namespace subordinate to the application's main namespace, in this case, Example.Properties, in a class called Settings. Notice that the properties relate to each configuration setting. An attribute decorating each property defines the scope in which the property resides and its default value. Because the Settings class inherits from the ApplicationSettingsBase class (a new collection class in the System.Configuration API), you can access the properties directly by name or you can enumerate all the configuration settings, which is what the ThisConfigEditor example does.

Enumerating Configuration Properties
The heart of the ThisConfigEditor's display is the private PopulateListView method in the ThisConfigEditor.cs code file.

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); } } }

Switching the private enum field _dt (which relates to the selection in the form's combo box determining which configuration properties to display: All, Application, or User) the code enumerates through the configuration properties. Each member of the collection is a SettingProperty instance (another new class in the System.Configuration API representing an individual configuration property) which in turn contains a collection of DictionaryEntry instances representing their various attributes. By iterating over this collection of attributes the code searches for matches to the appropriate scope-designating attribute to reflect the selection made in the combo box. After making a match, the code displays the configuration property by creating a new ListViewItem and adding it to the ListView on the form.

Author's Note: The ponderous nature of this method reflects certain instabilities in the current .NET beta 1 as much as a desire for readability.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap