Click here to Skip to main content
Click here to Skip to main content

Creating a Custom Settings Provider

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


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.


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"?>
    <WindowSize>300, 100</WindowSize>
    <WindowLocation>100, 100</WindowLocation>
    <WindowSize>300, 200</WindowSize>
    <WindowLocation>300, 300</WindowLocation>

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.


  • 18 October 2007 - Initial article.


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


About the Author

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.

Comments and Discussions

GeneralMy vote of 5 Pinmemberbasixrox2-Feb-15 3:51 
AnswerHaving trouble with Reload() or Reset() not loading new values? Here's the answer! PinmemberQuinxy von Besiex18-Nov-12 17:58 
GeneralRe: Having trouble with Reload() or Reset() not loading new values? Here's the answer! PinmemberWerner van Deventer24-Jan-15 9:52 
QuestionVS 2010 PinmemberErik Leo7-Jun-12 1:08 
QuestionBad markup in generated XML PinmemberEric Legault28-Mar-12 19:40 
QuestionGenerates a Form Designer Error and just cant seem to get around it... PinmemberBruceL25-Oct-11 10:55 
This code is something I have been looking for for a very long time. I was so excited to get this in place and working, but after doing the work needed my Main form simply will not open. We run Visual Studio 2010 with DevExpress, and no matter what I do (eg, tried all the suggestions posted here) I just keep getting the Form Designer error and I have to go back, remove all the Provider values, and then the form opens again.
Am I correct that using this code, I should be able to use the Settings Designer? ...Or is this JUST for runtime type usage?
The original post mentions "and provide the must override Application Name property"... Is this already in the code being posted? It appears to be, but am I missing something there?
I would REALLY love to get this great piece of code working - the biggest complaint we get from our field guys is how awful the Configuration values are to work with because they can never find them as Microsoft has WAY-over-complicated the whole Configuration and Settings engine. For our needs at least.
Bearing in mind that I tried every suggestion posted here just to get that Main form to work, and nothing resolves the issue, any suggestions?
Thanks so much either way - this is really great code, just wish I could get it to work.
AnswerRe: Generates a Form Designer Error and just cant seem to get around it... Pinmemberaron@versatel.nl18-Feb-13 7:37 
GeneralRe: Generates a Form Designer Error and just cant seem to get around it... PinmemberBruceL19-Feb-13 6:06 
GeneralMy vote of 5 PinmemberOneSpeed4-Nov-10 20:48 
GeneralI get null reference exception when I specify custom settings provider Pinmemberruslanv19-Jul-10 9:51 
QuestionHow to deal with null in propVal.SerializedValue Pinmemberbalistof215-Jun-10 6:05 
QuestionHow to set Custom Provider to just a few settings? Pinmembertinatran308-Jun-10 18:04 
Questionmore than one setting provider? PinmemberRejditsch11-Nov-09 2:17 
AnswerRe: more than one setting provider? PinmemberRejditsch11-Nov-09 3:46 
GeneralFixed c# version Pinmembergpgemini21-Feb-09 4:58 
GeneralRe: Fixed c# version PinmemberTommy Joe26-Mar-10 2:57 
GeneralRe: Fixed c# version PinmemberDotNetSlut17-Dec-10 5:40 
GeneralRe: Fixed c# version PinmemberMuffon21-Mar-12 9:53 
SuggestionRe: Fixed c# version [modified] PinmemberdeBUGer!15-Feb-14 4:35 
GeneralAdd dll please Pinmembergpgemini21-Feb-09 2:33 
GeneralRe: Add dll please Pinmembervbjay.net3-Jul-10 7:46 
QuestionHave you seen the DSL Designer for Custom Settings on codeplex? Pinmemberpibos24-Oct-08 6:16 
AnswerRe: Have you seen the DSL Designer for Custom Settings on codeplex? PinmemberBruceL25-Oct-11 10:29 
GeneralWriting Settings file to a different location Pinmemberrctaubert16-Jul-08 14:23 
GeneralRe: Writing Settings file to a different location Pinmembervbjay.net1-Apr-10 9:20 

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.150327.1 | Last Updated 18 Oct 2007
Article Copyright 2007 by CodeChimp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid