Click here to Skip to main content
15,880,405 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

Isolated Storage in Windows Phone 8

Rate me:
Please Sign up or sign in to vote.
4.70/5 (6 votes)
19 Dec 2013CPOL 16.7K   3   2
This tip shows you how to use the Isolated Storage in Windows Phone 8.

Introduction

This tip shows you how to use the Isolated Storage in order to store and retrieve Application Settings like the name or the Identifier of the user of the current application.

Using the Code

Before we start coding, we must include System.IO.IsolatedStorage.

C#
using System.IO.IsolatedStorage; 

Together, we will build an application where the user puts a number and a string.

XML
<Grid x:Name="ContentPanel" Margin="24,133,0,28" Grid.RowSpan="2">
    <TextBlock HorizontalAlignment="Left" Margin="75,20,0,0" 
       TextWrapping="Wrap" VerticalAlignment="Top" 
       RenderTransformOrigin="0.231,0.624">
        	<Run Text="Give  a number"/>
        	<Run Text=":"/>
    </TextBlock>
    <TextBox x:Name="Entier" HorizontalAlignment="Left" Height="72" 
      Margin="63,52,-63,0" TextWrapping="Wrap" Text="" 
      VerticalAlignment="Top" Width="456"/>
    <TextBlock HorizontalAlignment="Left" Margin="75,155,0,0" 
      TextWrapping="Wrap" VerticalAlignment="Top">
        	<Run Text="Give a string"/>
        	<Run Text=": "/>
    </TextBlock>
    <TextBox x:Name="chaine" HorizontalAlignment="Left" 
      Height="72" Margin="63,205,-63,0" TextWrapping="Wrap" 
      Text="" VerticalAlignment="Top" Width="456"/>

    <Button Content="save " HorizontalAlignment="Left" Margin="63,299,0,0" 
      VerticalAlignment="Top" Click="Button_Click_1" 
      RenderTransformOrigin="0.461,-2.292"/>
    <Button Content="put" HorizontalAlignment="Left" 
      Margin="234,299,0,0" VerticalAlignment="Top" Click="Button_Click_2"/>
</Grid>

Store Application Settings

If we want to store the number and the string entered by the user, we click upon a buton called "PUT" as shown in the following example:

C#
private void Button_Click_2(object sender, RoutedEventArgs e)
{
    IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
    int integerValue;
    int.TryParse(Entier.Text, out integerValue);
    //store the integer
    isoStoreSettings["IntegerValue"] = integerValue;
    //store the string
    isoStoreSettings["StringValue"] = chaine.Text;
} 

Retrieve Application Settings

Now we want to retrieve the application settings, that is why we click upon the button "PUT"which has the following code:

C#
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
    int integerValue;
    string stringValue;
    //Retreive The integer
    if (isoStoreSettings.TryGetValue("IntegerValue", out integerValue))
    {
        Entier.Text = integerValue.ToString();
    }
    //Retreive the string
    if (isoStoreSettings.TryGetValue("StringValue", out stringValue))
    {
        chaine.Text = stringValue;
    }
    isoStoreSettings.Save();
} 

Any comments are welcome.

License

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


Written By
Student
Tunisia Tunisia
Microsoft Certified Professional, Big Data and Cloud Architect.
Mail : hadrichmed@gmail.com

Comments and Discussions

 
QuestionisoStoreSettings.Save(); not necessary. Pin
6T9Fan9-Jan-14 5:21
6T9Fan9-Jan-14 5:21 
AnswerRe: isoStoreSettings.Save(); not necessary. Pin
Hadrich Mohamed9-Jan-14 8:01
professionalHadrich Mohamed9-Jan-14 8:01 
Many thanks Smile | :) )
Hadrich

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.