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" />

6.  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;

7.  Press F5 to run the application :

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

9.  Now close the application.

10.  Again press F5 and run the application.

Thanks and Have Fun!!!!!