Click here to Skip to main content
15,908,175 members
Articles / Programming Languages / Visual Basic
Article

Changing application-scoped settings at run time

Rate me:
Please Sign up or sign in to vote.
3.17/5 (9 votes)
16 Jun 20072 min read 86.5K   1.1K   29   15
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.

VB.NET
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


Written By
Web Developer
Georgia Georgia
Lecturer in Gori University (Georgia)

Comments and Discussions

 
PraiseMany thanks Pin
Gary_Corbell14-Feb-21 18:40
Gary_Corbell14-Feb-21 18:40 
SuggestionCan't be used with UAC or a standard user but may be useful in an installer class Pin
reeselmiller222-Mar-13 12:50
reeselmiller222-Mar-13 12:50 
GeneralMy vote of 2 Pin
Sergiy Sakharov6-May-11 0:24
Sergiy Sakharov6-May-11 0:24 
QuestionTrouble seeing how to store application-scoped settings Pin
bobishkindaguy13-Mar-10 8:52
bobishkindaguy13-Mar-10 8:52 
GeneralSimpler way Pin
Martin Konicek6-Aug-07 23:26
Martin Konicek6-Aug-07 23:26 
GeneralRe: Simpler way Pin
riezebosch14-May-09 21:05
riezebosch14-May-09 21:05 
GeneralRe: Simpler way Pin
brightmohan18-Mar-10 17:09
brightmohan18-Mar-10 17:09 
GeneralRe: Simpler way Pin
Member 74095411-Feb-11 17:24
Member 74095411-Feb-11 17:24 
QuestionShouldn't the initial value be different on next executionn? Pin
jmsnyc15-Jul-07 15:46
jmsnyc15-Jul-07 15:46 
AnswerRe: Shouldn't the initial value be different on next executionn? Pin
jmsnyc15-Jul-07 16:59
jmsnyc15-Jul-07 16:59 
GeneralFatal Flaw Pin
Paul A. Howes18-Jun-07 14:56
Paul A. Howes18-Jun-07 14:56 
GeneralRe: Fatal Flaw Pin
MPROCTOR21-Jun-07 13:58
MPROCTOR21-Jun-07 13:58 
GeneralRe: Fatal Flaw Pin
Paul A. Howes22-Jun-07 2:59
Paul A. Howes22-Jun-07 2:59 
GeneralRe: Fatal Flaw PinPopular
lpgray6-Jul-07 2:42
lpgray6-Jul-07 2:42 
Paul A. Howes wrote:
If an administrator wants to change an application-scoped setting, all he has to do is edit the configuration file and save it, as he should have the file system permissions to do so. It's not that big of a deal.


Every few months I do a search for ways to modify app.config. Every time I'm amazed at how few results I get, and I'm never satisfied with the solutions (they are usually too reliant on accessing property names by string values, or just not very flexible for reuse).

What really amazes me each time is when people answer with "You shouldn't do that" or "All you need to do is edit the config file."

You are absolutely right that a non-admin user doesn't and shouldn't have access to modify app.config. But it's perfectly reasonable to expect an admin to be able to make changes easily through a GUI interface rather than require him to edit a config file. Also, what about service applications? We should be able to create a service manager GUI application that can easily change the service's configuration.

Exceptions are intended to be used when something really bad happens, not as a verification mechanism.


Absolutely. That doesn't change the fact that the programmer should have the ability to write to app.config, though. The programmer just needs to use other means to determine if the user should be able to write to it.

There's a huge history of bad code out there that can be blamed on programmers who do not understand the difference between application and user settings. That doesn't mean that those of us who do understand it should be limited in what we can do.

Sorry to respond to your particular post, Paul. I'm not picking on any one individual. I just happened to be doing my usual search after getting frustrated with my own attempt (again) at an elegant solution, and needed to vent a little!

Lee
GeneralGreat Job! Pin
Tom Murdock18-Jun-07 13:25
Tom Murdock18-Jun-07 13:25 

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

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