Partager via


How to: Store and Retrieve Application Settings Using Isolated Storage

Microsoft Silverlight will reach end of support after October 2021. Learn more.

You can use isolated storage in Silverlight 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, application settings are stored locally on the user's computer. In Windows 7, 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.

NoteNote:

If you have multiple instances of an out-of-browser application running on the same computer and you use IsolatedStorageSettings, you may experience unexpected behavior when reading and saving settings. If this is a scenario for your application, consider using the IsolatedStorageFile APIs instead of IsolatedStorageSettings.

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.

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

    appSettings.Add("email", "someone@contoso.com")
    
    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.

    tbResults.Text = CType(appSettings("email"),String)
    
    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.

    appSettings("email") = "someone@fabrikam.com"
    
    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.

    appSettings.Remove("email")
    
    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.

<UserControl x:Class="SilverlightApplication_IsoStore_Again.Page"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://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>
<UserControl x:Class="SilverlightApplication_IsoStore_VB.Page"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://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>
    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
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