Login | Register   
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
 

How to Make Your .NET Windows Forms Configuration Files Dynamic  : Page 4

Although Windows Forms configuration files are read-only within the framework, they become dynamic XML databases when you create this AppSettingsManager class.


advertisement
Creating, Altering, and Deleting Settings
The AppSettingsManager class has a number of methods for altering the settings in the <appSettings> section. These are listed in Table 1. Although many of the methods accept an Object or Object array as parameters rather than simple strings, the types passed or requested must be convertible to or from string representations, because the values must be stored in string form in the XML-based configuration file. If the AppSettingsManager cannot convert the value to or from a String successfully, it throws an InvalidCastException.

Table 1.
Method Signature Description

Public Sub AbandonChanges()

Abandons any changes made to the configuration file since the last call to Save(), or if Save() has not been called, since the start of the application.

Public Sub AddValue( _
    ByVal aKey As String, _
    ByVal newValue As Object)
Adds a new value to the specified key. If the key exists, it appends the new value to the current value as an item in a comma-separated list. If the key does not exist, it creates it and sets the value to the new value. If the new value already exists in the specified key, the method leaves it alone.
Public Sub AddValues( _
    ByVal aKey As String _
    ByVal newValues() as object)
Adds the new values to the specified key. If the key exists, it appends the new values to the current value as items in a comma-separated list. If the key does not exist, it creates it, and sets the value to a comma-separated list of the new values. If a value already exists in the specified key, the method leaves it alone.
Public Function GetManager() _
    As AppSettingsManager
Returns the AppSettingsManager object.
Public Function GetValue( _
    ByVal aKey As String) _
    As String
Returns a string representation of the value associated with the specified key, or Nothing if the key does not exist.
Public Function GetValue( _
    ByVal aKey As String, _
    ByVal aType As _
    System.Type) As Object
Returns an Object cast to the specified type and associated with the specified key, or Nothing if the key does not exist.
Public Function GetValues( _
    ByVal aKey As String, _
    ByVal aType As _
    System.Type) As Object()
Returns an array of Objects cast to the specified type and associated with the specified key, or Nothing, if the key does not exist.
Public ReadOnly Property _
    HasPendingChanges() _
    As Boolean
Returns a Boolean value indicating whether the <appSettings> values have changed since the last call to Save() or, if Save() has not been called, since the start of the application.
Public ReadOnly Property _
    HasSettingChanged(ByVal _
    aKey As String) As Boolean
Returns a Boolean value indicating whether the value of the setting at the specified key has changed since the start of the application.
Public ReadOnly Property _
    KeyExists(ByVal aKey _
    As String) As Boolean
Returns a Boolean value indicating whether the specified key exists.
Public Sub Remove( _
    ByVal aKey As String)
Removes the specified key from the application settings. If the key does not exist, the AppSettingsManager ignores the call.
Public Sub RemoveValue( _
    ByVal aKey As String, _
    ByVal aValue As Object)
Removes the specified object from the specified key.
Public Sub Replace( _
    ByVal aKey As String, _
    ByVal oldValue As Object, _
    ByVal newValue As Object)
Replaces the specified existing value in the specified key with the new value. If either the specified key or the oldValue parameter does not exist, the AppSettingsManager throws an exception.
Public Sub Save() Saves the current settings to the application's configuration file. After calling Save(), any call to HasPendingChanges returns False until you make further changes to the settings values.
Public ReadOnly Property _
    ValueExists( _
    ByVal aKey As String, _
    ByVal aValue As Object) _
    As Boolean
Returns a Boolean value indicating whether the specified value exists in the specified key.
Public Function _
    GetAppSettingsElement() _
    As XmlElement
Returns an XmlElement that represents the <appSettings> tag, including any unsaved modifications. This method was included for demonstration purposes only, and should be removed from a production version of the class.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap