Introduction
In this article we are going to explore how to save and read a text file from isolated storage. Further in details we have to save the text file to the isolated storage through Windows Phone 7 and later we will read that file from isolated storage from Windows Phone 7. In this article we will also learn how to save the text file inside the directory or in a folder and after saving it into isolated storage we will retrieve that text file which is inside the directory to the Windows Phone TextBlock.
Now to accomplish that we will use the IsolatedStorageFile class to create a virtual space used to store that text file and IsolatedStorageFileStream class is used to do operations related to save, read, open, create the text file and another class named as StreamWriter to write that text to the text file and the class named as StreamReader to read the text from the text file which is stored inside the isolated storage. Further, to do that you have to follow the steps which is given below. In such type of storage, data is stored in compartments that are isolated by the current user and by the assembly in which the code exists. Additionally, data can be isolated by domain. Roaming profiles can be used in conjunction with isolated storage so isolated stores will travel with the user's profile.
Step 1: In this step, first of all we have to open a Windows Phone application; let us see how you will open it.
- Go to Visual Studio 2010
- File->New->Project
- Select the template named as silverlight for Windows Phone
- Select the Windows Phone application
- Give it a name as you want.
![]()

Step 2: In this step we will see that there are some important resources or namespaces you have to added to the MainPage.xaml.cs file; let us see the namespaces which are given below.

Step 3: In this step we will see the code for the first button named btnSvfile_Click which is given below.
Code:
private void btnSvfile_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//create new file
using (StreamWriter SW = new StreamWriter(new IsolatedStorageFileStream("Info.txt", FileMode.Create, FileAccess.Write, ISF)))
{
string text = "Hi this is the text which will be written to the file and we can retrieve that later";
SW.WriteLine(text);
SW.Close();
MessageBox.Show("text has been saved successfully to the file");
}
}
Step 4: In this step we will see the code for the second button named btnRdfile_Click which is given below.
Code:
private void btnRd_file_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream FS = ISF.OpenFile("Info.txt", FileMode.Open, FileAccess.Read);
using (StreamReader SR = new StreamReader(FS))
{
this.text.Text = SR.ReadLine();
}
}
Step 5: In this step we will see the code for the third button named btnwrfile_Click which is given below.
Code:
private void btnwt_fileClick(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
IsolatedStorageFileStream FS = ISF.OpenFile("Info.txt", FileMode.Open, FileAccess.Write);
using (StreamWriter SW = new StreamWriter(FS))
{
string text = "Here we have added more text about amit maheshwari which is working with MCN Solutions pvt ltd !";
SW.Write(text);
SW.Close();
}
}
Step 6: In this step we will see the code for the fourth button named btnwt_Directory_Click which is given below.
Code:
private void btnwt_Directory_Click(object sender, RoutedEventArgs e)
{
//By using it we will obtain the virtual store for application
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//Now we have to create a new folder and call it "MyFolder"
ISF.CreateDirectory("MyFolder");
//Here we will Create a new file and assign a StreamWriter to the store and this new file (Info1.txt)
//In this Also take the text contents from the text control and write it to Info1.txt
StreamWriter SW = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\Info1.txt", FileMode.OpenOrCreate, ISF));
string text = "Hi this is the text which will be written to the file and which is in the IsolatedStorage!";
SW.WriteLine(text);
SW.Close();
MessageBox.Show("Text written to the directory successfully");
}
Step 7: In this step we will see the code for the fifth button named btnRd_Directory_Click which is given below.
Code:
private void btnRd_Directory_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream FS = ISF.OpenFile("MyFolder\\Info1.txt", FileMode.Open, FileAccess.Read);
using (StreamReader SR = new StreamReader(FS))
{
this.text1.Text = SR.ReadLine();
}
}
Step 8: In this step we will see the complete code for the MainPage.Xaml.cs file which is given below.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
namespace mytextfile
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnwt_fileClick(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
IsolatedStorageFileStream FS = ISF.OpenFile("Info.txt", FileMode.Open, FileAccess.Write);
using (StreamWriter SW = new StreamWriter(FS))
{
string text = "Here we have added more text about amit maheshwari which is working with MCN Solutions pvt ltd !";
SW.Write(text);
SW.Close();
}
}
private void btnSvfile_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//create new file
using (StreamWriter SW = new StreamWriter(new IsolatedStorageFileStream("Info.txt", FileMode.Create, FileAccess.Write, ISF)))
{
string text = "Hi this is the text which will be written to the file and we can retrieve that later";
SW.WriteLine(text);
SW.Close();
MessageBox.Show("text has been saved successfully to the file");
}
}
private void btnRd_file_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream FS = ISF.OpenFile("Info.txt", FileMode.Open, FileAccess.Read);
using (StreamReader SR = new StreamReader(FS))
{
this.text.Text = SR.ReadLine();
}
}
private void btnwt_Directory_Click(object sender, RoutedEventArgs e)
{
//By using it we will obtain the virtual store for application
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
//Now we have to create a new folder and call it "MyFolder"
ISF.CreateDirectory("MyFolder");
//Here we will Create a new file and assign a StreamWriter to the store and this new file (Info1.txt)
//In this Also take the text contents from the text control and write it to Info1.txt
StreamWriter SW = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\Info1.txt", FileMode.OpenOrCreate, ISF));
string text = "Hi this is the text which will be written to the file and which is in the IsolatedStorage!";
SW.WriteLine(text);
SW.Close();
MessageBox.Show("Text written to the directory successfully");
}
private void btnRd_Directory_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream FS = ISF.OpenFile("MyFolder\\Info1.txt", FileMode.Open, FileAccess.Read);
using (StreamReader SR = new StreamReader(FS))
{
this.text1.Text = SR.ReadLine();








Join the conversation! Your thoughts help the community grow.