Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Changing application-scoped settings at run time

0.00/5 (No votes)
16 Jun 2007 2  
How to change Application-scoped settings programmatically

Screenshot - Application_scoped_settings.gif

Overview

It is well known that application settings are a Microsoft Visual Studio 2005 feature that replace the dynamic properties feature in the previous version. Application settings allow you to store and retrieve property settings and other information for your application dynamically. You can read a setting by accessing the setting's property on the My.Settings object. The My.Settings object exposes each setting as a property. The property name is the same as the setting name, and the property type is the same as the setting type.

There are two types of application settings, based on scope: user-scoped and application-scoped settings. The setting's scope indicates if the property is read-only; the property for an Application scope setting is read-only, while the property for a User scope setting is read-write. Therefore you can change and save the values of user-scoped settings at run time, whereas you change application-scoped settings usually when creating the application, through the Project Designer, or by editing the application's configuration file.

But nevertheless, application-scoped settings can be changed programmatically as needed. For that let's remember that the project system stores application-scoped settings in an application's configuration file, which is a standard XML file and is created at design time when you create the first application setting. Thus to change the application-scoped setting at run time, you must programmatically change the corresponding record in the application's configuration file with the <applicationSettings> tag.

Using the code

Suppose that we have an application-scoped string type setting MyAppScopedSetting in our project, the procedure ChangeMyAppScopedSetting defined below changes this settings value by newValue.

Private Sub ChangeMyAppScopedSetting(ByVal newValue As String)
Dim config As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim xmlDoc As New XmlDocument()

' Load an XML file into the XmlDocument object.

Try
xmlDoc.PreserveWhitespace = True
xmlDoc.Load(config.FilePath.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try

Dim i, j, k, l As Int32
Try
For i = 0 To xmlDoc.GetElementsByTagName("applicationSettings").Count - 1
For j = 0 To xmlDoc.GetElementsByTagName_
    ("applicationSettings").Item(i).ChildNodes.Count - 1
For k = 0 To xmlDoc.GetElementsByTagName_
    ("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Count - 1
If xmlDoc.GetElementsByTagName_
   ("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item(k).Name _
     = "setting" Then
If xmlDoc.GetElementsByTagName_
    ("applicationSettings").Item(i).ChildNodes.Item(j).ChildNodes.Item_
    (k).Attributes.Item(0).Value = "MyAppScopedSetting" Then
For l = 0 To xmlDoc.GetElementsByTagName("applicationSettings")_
    .Item(i).ChildNodes.Item(j).ChildNodes.Item(k).ChildNodes.Count - 1
If xmlDoc.GetElementsByTagName("applicationSettings").Item(i)_
    .ChildNodes.Item(j).ChildNodes.Item(k).ChildNodes.Item(l).Name = _
    "value" Then
xmlDoc.GetElementsByTagName_
    ("applicationSettings").Item(i).ChildNodes.Item(j)._
    ChildNodes.Item(k).ChildNodes.Item(l).InnerText = newValue
End If
Next l
End If
End If
Next k
Next j
Next i
xmlDoc.Save(config.FilePath.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Don't forget to reference System.Configuration namespace and to import System.Xml and System.Configuration namespaces in your project.

Notes

I have tested this project under VS.NET 2005 and Windows XP SP2.

Contact me

My name is Levan Midodashvili and you can contact me by email at levmid@hotmail.com or levmid@yahoo.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here