Silverlight
How to: Store and Retrieve Application Settings Using Isolated Storage

You can use isolated storage in Silverlight version 2 to store application settings, which are per-application, per-computer, and per-user settings. Their scope is determined by the full path of the application .xap file. For example, you can store application settings, such as number of images to display per page, page layout customization settings, and so on.

You store application settings using isolated storage by using members of the IsolatedStorageSettings class. This class implements a dictionary that is designed to store name/value pairs.

Like all data stored in isolated storage using Silverlight 2, application settings are stored locally on the user's computer. In Windows Vista, the information is stored in the AppData\LocalLow directory. For other operating system versions, including those on the Apple Macintosh, information is stored in the AppData\Local directory. Values stored in isolated storage are stored locally and are not available for roaming.

To create an application settings dictionary and add an entry

  1. Create a new instance of the IsolatedStorageSettings class, as shown in the following example:

    NoteNote:

    You might have to add a reference to the System.IO.IsolatedStorage namespace in your code.

    Visual Basic
    Private appSettings As IsolatedStorageSettings = _
               IsolatedStorageSettings.ApplicationSettings

    C#
    private IsolatedStorageSettings appSettings = 
               IsolatedStorageSettings.ApplicationSettings;
  2. Call the Add method to add a key/value pair into the dictionary.

    Visual Basic
    appSettings.Add("email", "someone@contoso.com")

    C#
    appSettings.Add("email", "someone@contoso.com");

To retrieve a value from the application settings dictionary

  • Reference the key and cast the IsolatedStorageSettings object to the correct type, as shown in the following example.

    Visual Basic
    tbResults.Text = CType(appSettings("email"),String)

    C#
    tbResults.Text = (string)appSettings["email"];

To change a value in the application settings dictionary

  • Reference the key and assign a new value, as shown in the following example.

    Visual Basic
    appSettings("email") = "someone@fabrikam.com"

    C#
    appSettings["email"] = "someone@fabrikam.com";

To delete an item in the application settings dictionary

  • Call the Remove method and pass the key of the item to remove, as shown in the following example.

    Visual Basic
    appSettings.Remove("email")

    C#
    appSettings.Remove("email");
Example

The following example creates an application settings dictionary using isolated storage, adds a key/value pair, retrieves the value, changes the value, and deletes the value.

XAML
<UserControl x:Class="SilverlightApplication_IsoStore_Again.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
      <StackPanel Height="230" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" >  
        <Button Height="30" Width="60" x:Name="bAdd" Content="Add"  Margin="10" 
                Click="bAdd_Click" ></Button>
        <Button Height="30" Width="60" x:Name="bRetrieve" Content="Retrieve" Margin="10" 
                Click="bRetrieve_Click" ></Button>
        <Button Height="30" Width="60" x:Name="bChange" Content="Change" Margin="10" 
                Click="bChange_Click" ></Button>
        <Button Height="30" Width="60" x:Name="bDelete" Content="Delete" Margin="10" 
                Click="bDelete_Click" ></Button>
        </StackPanel>
        <TextBox Height="50" Width="280" Background="Beige" x:Name="tbResults" 
                 BorderBrush="Beige" BorderThickness="5"  
                 FontFamily="Arial" FontSize="12" Text="Silverlight Test Area" 
                 HorizontalAlignment="Right"></TextBox>
    </Grid>
</UserControl>
XAML
<UserControl x:Class="SilverlightApplication_IsoStore_VB.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Height="230" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" >
            <Button Height="30" Width="60" x:Name="bAdd" Content="Add"  Margin="10" 
                Click="bAdd_Click" ></Button>
            <Button Height="30" Width="60" x:Name="bRetrieve" Content="Retrieve" Margin="10" 
                Click="bRetrieve_Click" ></Button>
            <Button Height="30" Width="60" x:Name="bChange" Content="Change" Margin="10" 
                Click="bChange_Click" ></Button>
            <Button Height="30" Width="60" x:Name="bDelete" Content="Delete" Margin="10" 
                Click="bDelete_Click" ></Button>
        </StackPanel>
        <TextBox Height="50" Width="280" Background="Beige" x:Name="tbResults" 
                 BorderBrush="Beige" BorderThickness="5"  
                 FontFamily="Arial" FontSize="12" Text="Silverlight Test Area" 
                 HorizontalAlignment="Right"></TextBox>
    </Grid>
</UserControl>
Visual Basic
    Private appSettings As System.IO.IsolatedStorage.IsolatedStorageSettings = _
                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Private Sub bAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            appSettings.Add("email", "someone@contoso.com")
            tbResults.Text = "Settings stored."
        Catch ex As ArgumentException
            tbResults.Text = ex.Message
        End Try
    End Sub

    Private Sub bRetrieve_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            tbResults.Text = ("Setting retrieved: " + CType(appSettings("email"), String))
        Catch ex As System.Collections.Generic.KeyNotFoundException
            tbResults.Text = ex.Message
        End Try
    End Sub

    Private Sub bChange_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        appSettings("email") = "someone@fabrikam.com"
        tbResults.Text = ("Changed to: " + CType(appSettings("email"), String))
    End Sub

    Private Sub bDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        appSettings.Remove("email")
        tbResults.Text = "Email deleted. Click Retrieve to confirm deletion."
    End Sub
End Class
C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;

namespace SilverlightApplication_IsoStore_Again
{
    public partial class Page : UserControl
    {
        private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        public Page()
        {
            InitializeComponent();
        }

        private void bAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                appSettings.Add("email", "someone@contoso.com");
                tbResults.Text = "Settings stored.";
            }
            catch (ArgumentException ex)
            {
                tbResults.Text = ex.Message;
            }
        }

        private void bRetrieve_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tbResults.Text = "Setting retrieved: " + (string)appSettings["email"];
            }
            catch (System.Collections.Generic.KeyNotFoundException ex)
            {
                tbResults.Text = ex.Message;
            }
        }

        private void bChange_Click(object sender, RoutedEventArgs e)
        {
            appSettings["email"] = "someone@fabrikam.com";
            tbResults.Text = "Changed to: " + (string)appSettings["email"];
        }

        private void bDelete_Click(object sender, RoutedEventArgs e)
        {
            appSettings.Remove("email");
            tbResults.Text = "Email deleted. Click Retrieve to confirm deletion.";
        }

    }
}
See Also

Reference

Tags :


Page view tracker