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
.
using System.IO.IsolatedStorage;
Together, we
will build an application where the user puts a number and a string.
<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:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
int integerValue;
int.TryParse(Entier.Text, out integerValue);
isoStoreSettings["IntegerValue"] = integerValue;
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:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
int integerValue;
string stringValue;
if (isoStoreSettings.TryGetValue("IntegerValue", out integerValue))
{
Entier.Text = integerValue.ToString();
}
if (isoStoreSettings.TryGetValue("StringValue", out stringValue))
{
chaine.Text = stringValue;
}
isoStoreSettings.Save();
}
Any comments are welcome.