How to read data from a text file in Isolated Storage in Windows Phone ?

In my previous blog post , i explained How to write data to a file in Isolated Storage in Windows Phone ?

In this blog post , will show you How to read data from a text file in Isolated Storage in Windows Phone that was written to the Isolated Storage .

How to read data from a text 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. Verify if the file exists in the Isolated Storage with the FileExists method of the IsolatedStorageFile class .

iso.FileExists("Employees.txt")

4. Open the File with the OpenFile function of the IsolatedStorageFile and assign it to the IsolatedStorageFileStream .

IsolatedStorageFileStream stream = iso.OpenFile("Employees.txt", System.IO.FileMode.Open);

5. Create an instance of the StreamReader with the IsolatedStorageFileStream that was created above as the parameter .

StreamReader streamReader = new StreamReader(stream);

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

textBox1.Text = streamReader.ReadToEnd();
streamReader.Close();
private void button2_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
if (iso.FileExists("Employees.txt"))
{
IsolatedStorageFileStream stream = iso.OpenFile("Employees.txt", System.IO.FileMode.Open);
StreamReader streamReader = new StreamReader(stream);
textBox1.Text = streamReader.ReadToEnd();
streamReader.Close();
}
}

Leave a Reply