Click here to Skip to main content
11,705,052 members (52,580 online)
Click here to Skip to main content

Creating a Custom Settings Provider

, 17 Oct 2007 CPOL 212.7K 5.7K 66
Rate this:
Please Sign up or sign in to vote.
Demonstration of how to write custom settings providers.

Introduction

This article demonstrates how to write a custom Settings Provider to allow you to persist your My.Settings to your own storage system. The example given creates an XML file in the application folder which is ideal for portable applications. It also includes brief details of how to implement storage onto a U3 compliant USB device.

Background

The My.Settings functionality introduced with .NET 2.0 saves a lot of time and repeated effort for developers by providing a common, easy to use, and well thought out method of loading/saving settings. For a standard WinForms application, this works great, separating local and roaming settings automatically and storing them in the Documents and Settings\User\Local Settings\Application Data and Documents and Settings\User\Application Data folder hierarchies, respectively. But, what happens if you haven't got a typical scenario, say you want settings to be stored on a network drive, or you want them to be portable with the application so they can run off a USB device? As with most things in the .NET framework, you can override this default behaviour thankfully. In this case, it's by writing your own Settings Provider. This sounds daunting, but is actually relatively trivial. All you have to do is create a new class that inherits from SettingsProvider and provide the must override ApplicationName property which I just default to the Product Name.

Using the Code

Using a custom settings provider is simple; just include the class within your application, and when viewing the My Project/Settings page, just set the Provider property (defaults to blank) to your provider class name. Note that the Provider property is set for each setting individually; that way, you could keep some items under the default .NET provider model but override a few critical ones to travel with you using this portable provider.

The screenshot below shows exactly where to change this:

Screenshot - CustomSettingsProvider1.jpg

By default, the roaming property is set to false, which for the portable settings provider will mean that the setting is machine specific. For settings like window size/location, this is desired, but for settings you want to be used regardless of machine (in this example, the UserWord), you should set this to true.

Other than that, handle the settings exactly as you would normally do. In the example app, we just get and set them on our Form Load and Closing events.

When you run the application, it tries to load the settings from an XML file named applicationtitle.settings. Since this does not exist to start with, it uses the default values. Upon exiting the application, the settings file is created. If you have a look at it (in the project's bin/debug folder), you'll see it has the following format;

<?xml version="1.0" encoding="utf-8"?>
<Settings>
  <WorkPC>
    <WindowSize>300, 100</WindowSize>
    <WindowLocation>100, 100</WindowLocation>
  </WorkPC>
  <HomePC>
    <WindowSize>300, 200</WindowSize>
    <WindowLocation>300, 300</WindowLocation>
  </HomePC>
  <UserWord>Test</UserWord>
</Settings>

Notice how the settings that have a roaming value of false are enclosed within an element for the current PC name, whilst the roaming setting of UserWord is at the Settings node level, meaning it is used regardless of the PC you are using.

Also included is a U3 specific settings provider. This inherits from PortableSettingsProvider, overriding the GetAppSettingsPath function which, in this case, tries to retrieve a path from an environment variable. If you're happy with the storing of settings within an XML file but just want to change the location (perhaps a common network mapping), then you can use this inheritance technique as well.

Points of Interest

The real guts of the Settings Provider happen in the GetPropertyValues and SetPropertyValues functions. These pass in a collection object of all properties required to be got/set for this provider, which we just iterate round, loading or saving them to our XML document. I have implemented a SettingsXML property which either loads or creates a new XML document with the required root node.

The final point of interest is the IsRoaming function. This looks through the attributes collection of the objects on the property for a type of SettingsManageabilityAttribute. This attribute being present indicates that the IsRoaming property is set to true.

History

  • 18 October 2007 - Initial article.

License

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

Share

About the Author

CodeChimp
Web Developer
United Kingdom United Kingdom
A general purpose analyst programmer working in a small development team for a company in the city.

VB has always been my passion and I code both Win forms and web based systems for both work and fun.

You may also be interested in...

Comments and Discussions

 
QuestionMultiple Providers Pin
EECAnalyzer6-Aug-15 13:07
memberEECAnalyzer6-Aug-15 13:07 
QuestionUpgrade doesn't work Pin
phil_the6-Jul-15 1:45
memberphil_the6-Jul-15 1:45 
GeneralMy vote of 5 Pin
basixrox2-Feb-15 2:51
memberbasixrox2-Feb-15 2:51 
AnswerHaving trouble with Reload() or Reset() not loading new values? Here's the answer! Pin
Quinxy von Besiex18-Nov-12 16:58
memberQuinxy von Besiex18-Nov-12 16:58 
GeneralRe: Having trouble with Reload() or Reset() not loading new values? Here's the answer! Pin
Werner van Deventer24-Jan-15 8:52
memberWerner van Deventer24-Jan-15 8:52 
QuestionVS 2010 Pin
Erik Leo7-Jun-12 0:08
memberErik Leo7-Jun-12 0:08 
QuestionBad markup in generated XML Pin
Eric Legault28-Mar-12 18:40
memberEric Legault28-Mar-12 18:40 
QuestionGenerates a Form Designer Error and just cant seem to get around it... Pin
BruceL25-Oct-11 9:55
memberBruceL25-Oct-11 9:55 
AnswerRe: Generates a Form Designer Error and just cant seem to get around it... Pin
aron@versatel.nl18-Feb-13 6:37
memberaron@versatel.nl18-Feb-13 6:37 
GeneralRe: Generates a Form Designer Error and just cant seem to get around it... Pin
BruceL19-Feb-13 5:06
memberBruceL19-Feb-13 5:06 
GeneralMy vote of 5 Pin
OneSpeed4-Nov-10 19:48
memberOneSpeed4-Nov-10 19:48 
GeneralI get null reference exception when I specify custom settings provider Pin
ruslanv19-Jul-10 8:51
memberruslanv19-Jul-10 8:51 
QuestionHow to deal with null in propVal.SerializedValue Pin
balistof215-Jun-10 5:05
memberbalistof215-Jun-10 5:05 
QuestionHow to set Custom Provider to just a few settings? Pin
tinatran308-Jun-10 17:04
membertinatran308-Jun-10 17:04 
Questionmore than one setting provider? Pin
Rejditsch11-Nov-09 1:17
memberRejditsch11-Nov-09 1:17 
AnswerRe: more than one setting provider? Pin
Rejditsch11-Nov-09 2:46
memberRejditsch11-Nov-09 2:46 
GeneralFixed c# version Pin
gpgemini21-Feb-09 3:58
membergpgemini21-Feb-09 3:58 
GeneralRe: Fixed c# version Pin
Tommy Joe26-Mar-10 1:57
memberTommy Joe26-Mar-10 1:57 
GeneralRe: Fixed c# version Pin
DotNetSlut17-Dec-10 4:40
memberDotNetSlut17-Dec-10 4:40 
GeneralRe: Fixed c# version Pin
Muffon21-Mar-12 8:53
memberMuffon21-Mar-12 8:53 
SuggestionRe: Fixed c# version Pin
deBUGer!15-Feb-14 3:35
memberdeBUGer!15-Feb-14 3:35 
GeneralAdd dll please Pin
gpgemini21-Feb-09 1:33
membergpgemini21-Feb-09 1:33 
GeneralRe: Add dll please Pin
vbjay.net3-Jul-10 6:46
membervbjay.net3-Jul-10 6:46 
QuestionHave you seen the DSL Designer for Custom Settings on codeplex? Pin
pibos24-Oct-08 5:16
memberpibos24-Oct-08 5:16 
AnswerRe: Have you seen the DSL Designer for Custom Settings on codeplex? Pin
BruceL25-Oct-11 9:29
memberBruceL25-Oct-11 9:29 
GeneralWriting Settings file to a different location Pin
rctaubert16-Jul-08 13:23
memberrctaubert16-Jul-08 13:23 
GeneralRe: Writing Settings file to a different location Pin
vbjay.net1-Apr-10 8:20
membervbjay.net1-Apr-10 8:20 
GeneralRe: Writing Settings file to a different location Pin
SSDiver21122-Jul-10 17:32
memberSSDiver21122-Jul-10 17:32 
GeneralRe: Writing Settings file to a different location Pin
vbjay.net2-Jul-10 22:53
membervbjay.net2-Jul-10 22:53 
GeneralRe: Writing Settings file to a different location Pin
SSDiver21123-Jul-10 6:07
memberSSDiver21123-Jul-10 6:07 
Thanks for the reply. Unfortunatly I was not able to unzip the download, so I looked at the code online. I copied the Settings.vb code to mine but it does not recognize some of the code like:

Dim at As Attribute = (From a As DictionaryEntry In prop.Attributes Where TypeOf a.Value Is System.Configuration.UserScopedSettingAttribute Select a.Value).FirstOrDefault

I am using VS2005, is this not compatible?

I am looking for a way to save My.Settings for an app running on a network drive.

Thanks again,
SSDiver2112
GeneralRe: Writing Settings file to a different location Pin
vbjay.net3-Jul-10 6:31
membervbjay.net3-Jul-10 6:31 
GeneralRe: Writing Settings file to a different location Pin
SSDiver21123-Jul-10 8:05
memberSSDiver21123-Jul-10 8:05 
GeneralRe: Writing Settings file to a different location Pin
SSDiver21126-Jul-10 10:40
memberSSDiver21126-Jul-10 10:40 
QuestionWin Form Designer breaking with custom SettingsProvider Pin
Tom Faller20-Jun-08 2:48
memberTom Faller20-Jun-08 2:48 
AnswerRe: Win Form Designer breaking with custom SettingsProvider [modified] Pin
GeoffHacker20-Jul-08 15:16
memberGeoffHacker20-Jul-08 15:16 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
Stewart McGuire10-Dec-08 10:22
memberStewart McGuire10-Dec-08 10:22 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
piles5-May-09 3:03
memberpiles5-May-09 3:03 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
Matteo Rossi4-Jun-09 5:17
memberMatteo Rossi4-Jun-09 5:17 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
juan321119-Oct-09 9:05
memberjuan321119-Oct-09 9:05 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
simonlynen26-Nov-09 3:56
membersimonlynen26-Nov-09 3:56 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
MLLong1-Feb-10 6:38
memberMLLong1-Feb-10 6:38 
GeneralRe: Win Form Designer breaking with custom SettingsProvider Pin
sybhs7-Jun-15 23:27
membersybhs7-Jun-15 23:27 
AnswerRe: Win Form Designer breaking with custom SettingsProvider Pin
Spock11-Aug-10 11:21
memberSpock11-Aug-10 11:21 
AnswerRe: Win Form Designer breaking with custom SettingsProvider Pin
bwknight8777-Oct-11 6:32
memberbwknight8777-Oct-11 6:32 
AnswerRe: Win Form Designer breaking with custom SettingsProvider Pin
Member 904782530-May-12 14:55
memberMember 904782530-May-12 14:55 
QuestionDoes it support .net 3.5 Pin
sachintana21-Jan-08 20:35
membersachintana21-Jan-08 20:35 
GeneralError using the project resources Pin
Snakeskinner6-Nov-07 2:58
memberSnakeskinner6-Nov-07 2:58 
GeneralRe: Error using the project resources Pin
CodeChimp7-Nov-07 22:57
memberCodeChimp7-Nov-07 22:57 
GeneralRe: Error using the project resources Pin
Snakeskinner9-Nov-07 0:50
memberSnakeskinner9-Nov-07 0:50 
AnswerRe: Error using the project resources Pin
hikariuk7-Apr-08 23:33
memberhikariuk7-Apr-08 23:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 18 Oct 2007
Article Copyright 2007 by CodeChimp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid