Namespace:
System.IO.IsolatedStorage
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public NotInheritable Class IsolatedStorageSettings _
Implements IDictionary(Of String, Object), _
ICollection(Of KeyValuePair(Of String, Object)), IEnumerable(Of KeyValuePair(Of String, Object)), _
IDictionary, ICollection, IEnumerable
Dim instance As IsolatedStorageSettings
public sealed class IsolatedStorageSettings : IDictionary<string, Object>,
ICollection<KeyValuePair<string, Object>>, IEnumerable<KeyValuePair<string, Object>>,
IDictionary, ICollection, IEnumerable
| Exception | Condition |
|---|
| ArgumentNullException |
key is nullNothingnullptra null reference (Nothing in Visual Basic). This exception is thrown when you attempt to reference an instance of the class by using an indexer and the variable you pass in for the key value is nullNothingnullptra null reference (Nothing in Visual Basic). |
IsolatedStorageSettings provide a convenient way to store user specific data as key-value pairs in a local IsolatedStorageFile. A typical use is to save settings, such as the number of images to display per page, page layout options, and so on.
User settings can be specific to an application or shared across applications in the same domain. ApplicationSettings are stored as per-application, per-computer, and per-user settings. Their scope is determined by the full path of the application .xap file. SiteSettings are stored as per-domain, per-computer, and per-user settings. Their scope is determined by the sub-domain that hosts the application .xap file. For example, an application at http://www.contoso.com/site1/application.xap will have different ApplicationSettings than an application at http://www.contoso.com/site2/application.xap. However, both applications will share the same SiteSettings because they are hosted on the same sub-domain.
Note: |
|---|
The .xap files are not accessed directly; they are hosted within a Web page. |
Like all data stored in isolated storage using Silverlight 2, IsolatedStorageSettings 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.
The following code example creates an isolated storage settings dictionary, gets a name from the user, stores the name, and then retrieves the name. The name is used in a greeting that is displayed when the page is launched. The code example also enables the user to change, view, and delete the stored name.
<UserControl x:Class="IsolatedStorageSample.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="Trebuchet MS" FontSize="11"
Width="500" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel VerticalAlignment="Top" >
<TextBlock x:Name="tbGreeting" FontSize="24" />
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Name: " VerticalAlignment="Center"/>
<TextBox x:Name="tbName" Width="230" Margin="0,0,5,0"/>
<Button x:Name="btnAddName" Content="Add" Click="btnAddName_Click" />
<Button x:Name="btnChangeName" Content="Change" Click="btnChangeName_Click" />
<Button x:Name="btnRemoveName" Content="Remove" Click="btnRemoveName_Click" />
</StackPanel>
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Settings: " VerticalAlignment="Center"/>
<Button x:Name="btnClear" Content="Clear" Click="btnClear_Click" />
<Button x:Name="btnCount" Content="Count" Click="btnCount_Click" />
<Button x:Name="btnKeys" Content="Get Keys" Click="btnKeys_Click" />
<Button x:Name="btnValues" Content="Get Values" Click="btnValues_Click" />
</StackPanel>
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Results: " VerticalAlignment="Center"/>
<TextBox x:Name="tbResults" Width="230" Margin="0,0,5,0"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
Imports System.IO.IsolatedStorage
Partial Public Class Page
Inherits UserControl
Private userSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings
Public Sub New()
InitializeComponent()
' Retrieve and set user name.
Try
Dim name As String = CType(userSettings("name"), String)
tbGreeting.Text = "Hello, " & name
Catch ex As System.Collections.Generic.KeyNotFoundException
' No preference is saved.
tbGreeting.Text = "Hello, World"
End Try
End Sub
Private Sub btnAddName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Try
userSettings.Add("name", tbName.Text)
tbResults.Text = "Name saved. Refresh page to see changes."
Catch ex As ArgumentException
tbResults.Text = ex.Message
End Try
End Sub
Private Sub btnChangeName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
userSettings("name") = tbName.Text
tbResults.Text = "Name changed. Refresh page to see changes."
End Sub
Private Sub btnRemoveName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
If userSettings.Remove("name") = True Then
tbResults.Text = "Name removed. Refresh page to see changes."
Else
tbResults.Text = "Name could not be removed. Key does not exist."
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
userSettings.Clear()
tbResults.Text = "Settings cleared. Refresh page to see changes."
End Sub
Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
tbResults.Text = "Count: " & userSettings.Count()
End Sub
Private Sub btnKeys_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder("Keys: ")
For Each k As String In userSettings.Keys
sb.Append(k & "; ")
Next
tbResults.Text = sb.ToString
End Sub
Private Sub btnValues_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder("Values: ")
For Each v As Object In userSettings.Values
sb.Append(v.ToString() & "; ")
Next
tbResults.Text = sb.ToString
End Sub
End Class
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;
namespace IsolatedStorageSample
{
public partial class Page : UserControl
{
private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
public Page()
{
InitializeComponent();
// Retrieve and set user name.
try
{
string name = (string)userSettings["name"];
tbGreeting.Text = "Hello, " + name;
}
catch (System.Collections.Generic.KeyNotFoundException)
{
// No preference is saved.
tbGreeting.Text = "Hello, World";
}
}
private void btnAddName_Click(object sender, RoutedEventArgs e)
{
try
{
userSettings.Add("name", tbName.Text);
tbResults.Text = "Name saved. Refresh page to see changes.";
}
catch (ArgumentException ex)
{
tbResults.Text = ex.Message;
}
}
private void btnChangeName_Click(object sender, RoutedEventArgs e)
{
userSettings["name"] = tbName.Text;
tbResults.Text = "Name changed. Refresh page to see changes.";
}
private void btnRemoveName_Click(object sender, RoutedEventArgs e)
{
if (userSettings.Remove("name") == true)
{
tbResults.Text = "Name removed. Refresh page to see changes.";
}
else
{
tbResults.Text = "Name could not be removed. Key does not exist.";
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
userSettings.Clear();
tbResults.Text = "Settings cleared. Refresh page to see changes.";
}
private void btnCount_Click(object sender, RoutedEventArgs e)
{
tbResults.Text = "Count: " + userSettings.Count();
}
private void btnKeys_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("Keys: ");
foreach (string k in userSettings.Keys)
{
sb.Append(k + "; ");
}
tbResults.Text = sb.ToString();
}
private void btnValues_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("Values: ");
foreach (Object v in userSettings.Values)
{
sb.Append(v.ToString() + "; ");
}
tbResults.Text = sb.ToString();
}
}
}
System..::.Object
System.IO.IsolatedStorage..::.IsolatedStorageSettings
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources