Click here to Skip to main content
Licence 
First Posted 16 Jun 2007
Views 36,170
Downloads 292
Bookmarked 28 times

Changing application-scoped settings at run time

By | 16 Jun 2007 | Article
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

About the Author

Levan Midodashvili

Web Developer

Georgia Georgia

Member

Lecturer in Gori University (Georgia)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberSergiy Sakharov0:24 6 May '11  
QuestionTrouble seeing how to store application-scoped settings PinmemberBobishKindaGuy8:52 13 Mar '10  
GeneralSimpler way Pinmemberinv_inv23:26 6 Aug '07  
GeneralRe: Simpler way Pinmemberriezebosch21:05 14 May '09  
GeneralRe: Simpler way Pinmemberbrightmohan17:09 18 Mar '10  
GeneralRe: Simpler way PinmemberMember 740954117:24 1 Feb '11  
QuestionShouldn't the initial value be different on next executionn? Pinmemberjmsnyc15:46 15 Jul '07  
AnswerRe: Shouldn't the initial value be different on next executionn? Pinmemberjmsnyc16:59 15 Jul '07  
GeneralFatal Flaw PinmemberPaul A. Howes14:56 18 Jun '07  
GeneralRe: Fatal Flaw PinmemberMPROCTOR13:58 21 Jun '07  
GeneralRe: Fatal Flaw PinmemberPaul A. Howes2:59 22 Jun '07  
GeneralRe: Fatal Flaw PinPopularmemberlpgray2:42 6 Jul '07  
GeneralGreat Job! PinmemberTom Murdock13:25 18 Jun '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 16 Jun 2007
Article Copyright 2007 by Levan Midodashvili
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid