Click here to Skip to main content
14,034,071 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 22 Mar 2008
Licenced CPOL

Dynamic User Settings

, 22 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Dynamic User Settings
Visual Studio makes it easy to add user and application scoped settings. The settings tab on your project properties will let you add settings at design time. You can decide the type, scope, and default value. There may be times however when you would like to add a user scoped property at run time. I will demonstrate how to do this.

Adding the Property:

  1. Create an instance of System.Configuration.SettingsAttributeDictionary.
  2. Create an instance of System.Configuration.UserScopedSettingsAttribute.
  3. Add the System.Configuration.UserScopedSettingsAttribute object to the System.Configuration.SettingsAttributeDictionary.
  4. Create an instance of System.Configuration.SettingsProperty passing the System.Configuration.SettingsAttributeDictionary into the contructor as well as the property name, default value, and type.
  5. Add the System.Configuration.SettingsAttributeDictionary to the My.MySettings.Default.Properties collection.

Example Code:

Private Sub AddProperty(ByVal propertyName As String, _
      ByVal defaultValue As String, ByVal propertyType As Type)
     Dim providerName As String = "LocalFileSettingsProvider"

     Dim attributes As New Configuration.SettingsAttributeDictionary()
     Dim attr As New Configuration.UserScopedSettingAttribute()
     attributes.Add(attr.TypeId(), attr)

     Dim prop As New Configuration.SettingsProperty( _
      New Configuration.SettingsProperty(propertyName, propertyType, _
     My.MySettings.Default.Providers(providerName), False, defaultValue, _
     Configuration.SettingsSerializeAs.String, attributes, False, False))

End Sub

Setting the Property Value:
Example Code:

My.MySettings.Default.Item(propertyName) = propertyValue
Getting the Property Value:

Example Code:

Accessing the Property at Next Run:

You may notice that next time you run your application that the properties you added were not loaded automatically. Only properties that exist in your My.MySettings.Default object are loaded when properties are loaded.

  1. Add your property.
  2. Reload your My.MySettings.Default object.

Example Code:

AddProperty(SavedPropertyNameTextBox.Text, String.Empty)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Trevor Tubbs
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSource Pin
Member 1066469630-Mar-14 9:21
memberMember 1066469630-Mar-14 9:21 
AnswerRe: Source Pin
Bronfman.I.P2-Sep-14 23:45
memberBronfman.I.P2-Sep-14 23:45 
GeneralGreat Work!!! Pin
rajesh vasan29-Apr-10 4:07
memberrajesh vasan29-Apr-10 4:07 
GeneralRe: Great Work!!! Pin
DBLWizard31-Dec-13 9:22
memberDBLWizard31-Dec-13 9:22 
GeneralSource Missing confermed! Pin
Strobe60009-Jan-09 9:51
memberStrobe60009-Jan-09 9:51 
QuestionSource missing Pin
sgaggerj16-May-08 2:32
membersgaggerj16-May-08 2:32 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190423.1 | Last Updated 22 Mar 2008
Article Copyright 2008 by Trevor Tubbs
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid