Subscribe to our YouTube Channel for more Videos

How to write data to a file in Isolated Storage in Windows Phone ?


An Windows Phone Application might need to store and access data .

One of the ways to store the data is the Isolated Storage .

An Windows Phone Application cannot access the data stored by another Windows Phone Application and hence the name “Isolated” .

A typical use of the Isolated Storage will be to store the Application settings or some Data for the App .

You can create a file and store it in the Isolated Storage of the Windows Phone 7 .

In this example , i will show you how to create a text file in isolated storage , write data to the saved in the isolated storage .

How to write data to a file in Isolated Storage in Windows Phone ?

1. Add the namespace IsolatedStorage

using System.IO.IsolatedStorage;

2. Retreive the userscope isolated storage that belongs to the App to the IsolatedStorageFile using the IsolatedStorageFile.GetUserStoreForApplication

IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();

3. Create the file with the iso.CreateFile function and assign it to the IsolatedStorageFileStream ;

IsolatedStorageFileStream stream = iso.CreateFile("Employees.txt");

4. Create an instance of the StreamWriter with the IsolatedStorageFileStream that was created above as the parameter .

StreamWriter streamWrite = new StreamWriter(stream);

5. Write the Data from the textbox to the StreamWriter and close the StreamWriter

streamWrite.Write(textBox1.Text);
streamWrite.Close();

6. You are done . The entered data in the textbox is saved to the text file in the isolated storage .

private void button1_Click_1(object sender, RoutedEventArgs e)
{
   IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
   IsolatedStorageFileStream stream = iso.CreateFile("Employees.txt");
   StreamWriter streamWrite = new StreamWriter(stream);
   streamWrite.Write(textBox1.Text);
   streamWrite.Close();
}
How to write data to a file in Isolated Storage in Windows Phone ?
How to write data to a file in Isolated Storage in Windows Phone ?

via WindowsPhoneRocks.com

 

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.