Page 1 of 1

Working with Isolated Storage Rate Topic: -----

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 25 August 2010 - 07:12 AM

In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it.

So first, we need to add these using statements to the top of the code(if they aren't already there):

using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;



Next we will have our form. Very basic. It has two Textboxes and a Button. The first TextBox will allow the user to type in some text. The Button will save the data to IsolatedStorage, then read the data from the file and put it into the second TextBox.

Here is the XAML:

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentGrid" Grid.Row="1">

    <TextBox 
        Name="txtText" 
        Height="128" 
        HorizontalAlignment="Left"
        Margin="8,71,0,0" 
        VerticalAlignment="Top" 
        Width="460" 
        TextWrapping="Wrap" 
        FontSize="16" />
            
    <Button 
        Name="btnSave" 
        Content="Save" 
        Click="btnSave_Click"
        Height="115" 
        HorizontalAlignment="Left" 
        Margin="62,203,0,0" 
        VerticalAlignment="Top" 
        Width="336" />

    <TextBlock 
        Name="textBlock1" 
        Text="Text to add to file:" 
        Height="30" 
        HorizontalAlignment="Left" 
        Margin="24,49,0,0" 
        VerticalAlignment="Top" />

    <TextBlock 
        Name="textBlock2"
        Text="Data currently in file:" 
        Height="30" 
        HorizontalAlignment="Left" 
        Margin="24,342,0,0"
        VerticalAlignment="Top" />

    <TextBox 
        Name="txtCurrentText" 
        Height="128" 
        HorizontalAlignment="Left"
        Margin="6,364,0,0" 
        VerticalAlignment="Top" 
        Width="460" 
        TextWrapping="Wrap" 
        FontSize="16" />
            
</Grid>



So now we move to the code. We need to create an IsolatedStorageFile object:

using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{

}



Since we will always be appending to the file, we need to check to make sure the file exists. If it doesn't exist, we need to create it.

// we need to check to see if the file exists
if (!isoStorage.FileExists(fileName))
{
    // file doesn't exist...time to create it.
    isoStorage.CreateFile(fileName);
}



Once we have done the FileExists check, we can now open the file. Once the file has been opened, we can now use the StreamWriter class to write to it.

// since we are appending to the file, we must use FileMode.Append
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
{
    // opens the file and writes to it.
    using (var fileStream = new StreamWriter(isoStream))
    {
        fileStream.WriteLine(txtText.Text);
    }
}



Note: notice that we opened the file using FileMode.Append. Because we used this mode, we can't read from the file, we can only write to it.

So to read from the file, we will need to open the file again, this time using FileMode.Open. We are going to do this in another using block. Once the file is opened, we use the StreamReader class to read it.

// you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need 
//   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
//   we are simply reading the file, we use FileMode.Open
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
{
    // opens the file and reads it.
    using (var fileStream = new StreamReader(isoStream))
    {
        txtCurrentText.Text = fileStream.ReadToEnd();
    }
}



And that's it. If you run the application, type in the first textbox, hit Save, the data will show up in the bottom textbox. Do it again, and you will see that it has appended to the file.

As you can see, writing to Isolated Storage is really easy.

Here is the entire code for the form...

using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WP7IsoStorageDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "MyTextfile.txt";

            using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // we need to check to see if the file exists
                if (!isoStorage.FileExists(fileName))
                {
                    // file doesn't exist...time to create it.
                    isoStorage.CreateFile(fileName);
                }

                // since we are appending to the file, we must use FileMode.Append
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
                {
                    // opens the file and writes to it.
                    using (var fileStream = new StreamWriter(isoStream))
                    {
                        fileStream.WriteLine(txtText.Text);
                    }
                }

                // you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need 
                //   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
                //   we are simply reading the file, we use FileMode.Open
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
                {
                    // opens the file and reads it.
                    using (var fileStream = new StreamReader(isoStream))
                    {
                        txtCurrentText.Text = fileStream.ReadToEnd();
                    }
                }
            }
        }
    }
}




Is This A Good Question/Topic? 4
  • +

Replies To: Working with Isolated Storage

#2 hartra34  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 26
  • Joined: 04-November 09

Posted 25 August 2010 - 08:39 AM

nice tutorial on a topic that can be a little hard to grasp, its much different to work with storage that you don't "directly" have access to.
Was This Post Helpful? 0
  • +
  • -

#3 foxhound_97  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

Posted 25 August 2010 - 01:27 PM

Superb, looks like Isolated Storage in WP7 works the same as IStorage in the desktop .net. Gotta love the .NET framework.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 25 August 2010 - 04:50 PM

View Postfoxhound_97, on 25 August 2010 - 04:27 PM, said:

Superb, looks like Isolated Storage in WP7 works the same as IStorage in the desktop .net. Gotta love the .NET framework.


It should also be similar, if not the same, in Silverlight....though I haven't tried it.
Was This Post Helpful? 0
  • +
  • -

#5 hartra34  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 26
  • Joined: 04-November 09

Posted 25 August 2010 - 04:52 PM

Yeah it's the same as the regular desktop silverlight, but much more necessary in win phone 7.

This post has been edited by hartra34: 25 August 2010 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1