Accessing the Isolated Local Storage : File « Data « Silverlight






Accessing the Isolated Local Storage

Accessing the Isolated Local Storage
 

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
     <Grid x:Name="LayoutRoot" Background="LightBlue">
        <Button x:Name="saveBtn" Content="Save"/>
         <TextBox x:Name="saveText"/>
        <Button x:Name="getBtn" Content="Retrieve"/>
        <TextBlock x:Name="getText" TextWrapping="Wrap"/>
    </Grid>
</UserControl>
//File: Page.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.IO;
using System.IO.IsolatedStorage;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
     {
        IsolatedStorageFile localStore;

        public MainPage()
         {
            InitializeComponent();
            saveBtn.Click += new RoutedEventHandler(saveBtn_Click);
            getBtn.Click += new RoutedEventHandler(getBtn_Click);
            localStore = IsolatedStorageFile.GetUserStoreForApplication();
         }
         void saveBtn_Click(object sender, RoutedEventArgs e){
            string[] files = localStore.GetFileNames("*.dat");
              if (files.Length != 0)
                   foreach (string name in files)
                     localStore.DeleteFile(name);
              IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.dat",FileMode.Create, localStore);
              StreamWriter sWriter = new StreamWriter(stream);
              sWriter.WriteLine(saveText.Text);
              sWriter.Close();
              stream.Close();
         }
         void getBtn_Click(object sender, RoutedEventArgs e){
              IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.dat",FileMode.Open, localStore);
              StreamReader sReader = new StreamReader(stream);
              getText.Text = sReader.ReadLine();
              sReader.Close();
              stream.Close();
         }
     }
}

   
  








Related examples in the same category

1.Opening a Local File from a Silverlight ApplicationOpening a Local File from a Silverlight Application