Application Settings in WPF

Posted by in WPF category on for Beginner level | Points: 250 | Views : 32368 red flag
Rating: 4.33 out of 5  
 3 vote(s)

You can create and access values which can stay from application session to application session. This values are called as Settings in WPF.

 Download source code for Application Settings in WPF


The Settings can have any type of information which we require to store somewhere and later on we can use it.  It's similar to Session Technics in Web Development.  We can store some information in settings and we can also change the same, even at run-time.

Consider Settings as some centralized storage location.

Basically, Settings have 4 Properties:

  1. Name   (Name of the Setting)
  2. Type   (Data Type of the Setting)
  3. Value   (The information which will get store in Setting)
  4. Scope  (User or Application)

The bit confusing thing over here is a property called Scope.  Scope has two option as User or Application.  I'll try to describe the same in similar way,

User - The Session is restricted to the User 
Application -  The Session is restricted to the Application

OK, good enough.  But is there any difference between this too?? Can be asked somewhere in interviews!!! Huh!!

Yes, User settings are read/write while Application Settings are read-only.  (aah!! now what is this!!!)

User Settings can be access or modify while your application is running (run-time).  When you modify the User Settings, it will get saved by the application and immediate affect will be taken place.

Application Settings are read-only means that you can't change the value of Application Settings at run-time.  You will to change the same at design-time.

Woh Woh!! enough of theory, now let's go and do some hands-on guys!!

So, How to create Settings (Application or User):

Note: I am using Visual Studio 2010 and VB.NET as Language

  1. Go to the Solution Explorer
  2. Double-click My Project
  3. Go to the Settings tab

Note : You will not find the same value appears here as in your Settings tab.  ( I have updated the Settings file for me )

4.  Change the settings as been seen in above picture.

5.  Go to your XAML file and paste the below code :

<Window x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525">
        <ListBox Margin="15,15,0,0" Name="listBox1" Height="78"
                 HorizontalAlignment="Left" VerticalAlignment="Top" Width="107">
            <ListBoxItem>New York</ListBoxItem>
        <Button Margin="15,106,110,130" Name="button1">Change User City</Button>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="136,15,0,0" Name="TextBlock1" Text="TextBlock" VerticalAlignment="Top" />

7.  Go to your source-code file and paste the below code :

VB.NET Code :
Class MainWindow
    Dim astring As String
    Public Sub New()


        Me.Title = My.Settings.UserName
        TextBlock1.Text = "Previously Selected City " + My.Settings.UserCity

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles button1.Click

        If Not listBox1.SelectedItem Is Nothing Then
            TextBlock1.Text = "Previously Selected City " + astring
            astring = CType(listBox1.SelectedItem, ListBoxItem).Content.ToString
            My.Settings.UserCity = astring
        End If
    End Sub
End Class
C#.NET Code : 

class MainWindow
	string astring;

	public MainWindow()

		this.Title = My.Settings.UserName;
		TextBlock1.Text = "Previously Selected City " + My.Settings.UserCity;


	private void  // ERROR: Handles clauses are not supported in C#
button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
		if ((listBox1.SelectedItem != null)) {
			TextBlock1.Text = "Previously Selected City " + astring;
			astring = ((ListBoxItem)listBox1.SelectedItem).Content.ToString;
			My.Settings.UserCity = astring;

8.  Press F5 to run the application :

9.  Now click on New York and Press Change User City button:

10.  Now close the application.

11.  Again press F5 and run the application.

Thanks and Have Fun!!!!!
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: John Doe
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India

Login to vote for this post.

Comments or Responses

Posted by: Sutotpal on: 2/8/2011 | Points: 25
It's a nice article for beginners...

Posted by: Naimishforu on: 2/8/2011 | Points: 25
Thanks Sutotpal :)
Posted by: Karthikanbarasan on: 2/11/2011 | Points: 25
I dnt see any images here!!!
Posted by: Naimishforu on: 2/11/2011 | Points: 25
Yes Karthik, there is some problem with where I have uploaded images.

Will update it soon.

Posted by: Karthikanbarasan on: 2/11/2011 | Points: 25
oh ok
Posted by: Naimishforu on: 2/11/2011 | Points: 25

I hope it's fine now :)

Posted by: Karthikanbarasan on: 2/11/2011 | Points: 25
Yeah i can see the images!!!

Login to post response

Comment using Facebook(Author doesn't get notification)