Objective
This article will explain,
- How to create a text file in IsolatedStorageFile of a SilverLight 2 application.
- How to write into a text file in IsoltaedStorageFile.
- How to read a text file from IsolatedStorageFile.
- How to delete a file from IsolatedStorageFile.
Step 1
Create a SilverLight application. By selecting File->New->Project->SilverLight-> SilverLight Application.Design the XAML page. I am creating three buttons for the purpose of Read, Write and Delete File. There are two text boxes. One to get filename input and other for displaying content from the file and saving content from that text box.
MainPage.Xaml
<UserControl 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″ ns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ x:Class=”FileReadingandWritingwithIsolatedStorage.MainPage” Width=”Auto” Height=”Auto” mc:Ignorable=”d”><Grid x:Name=”LayoutRoot” Height=”400″ Width=”600″> <Grid.Background> <LinearGradientBrush EndPoint=”0.5,1″ StartPoint=”0.5,0″> |
Step 2
Now, writing the code behind to handle the Read and Write Operation. I am using IsolatedStorageFIle class to perform file handling operations.
IsolatedStorageFile
- This class is inside the namespace System.IO.IsolatedStorage
- We could set domain of IsolatedStorageFile either for SilverLight website or for SilverLight Application.
-
There are many methods exist inside this to work with File operations. For example, create directory, create file, delete directory, delete file etc.
How to write into the file?
private void btnWrite_Click(object sender, System.Windows.RoutedEventArgs e){using (var store = IsolatedStorageFile.GetUserStoreForApplication(){ if (!(store.FileExists(txtFileName.Text))){ MessageBox.Show(“File does not exist , we are creating one for you “); IsolatedStorageFileStream file = store.CreateFile(txtFileName.Text); file.Close();} using (StreamWriter sw = new StreamWriter(store.OpenFile(txtFileName.Text, FileMode.Open, FileAccess.Write))) { sw.WriteLine(txtContent.Text); } txtContent.Text = “”; txtFileName.Text = “”; |
Explanation
- StreamWriter is being used to write into the file.
- StreamWriter is inside the namespace System.IO.
- We are opening the ISolatedStorageFile for the SilverLight Application.
- We are checking, that if file name provided by user does not exist then create a new one with the provided name.
-
We are opening the file in Write Mode and writing the stream into that.
How to Read from the file?
private void btnRead_Click(object sender, System.Windows.RoutedEventArgs e){using (var store = IsolatedStorageFile.GetUserStoreForApplication(){ if (!(store.FileExists(txtFileName.Text))){ MessageBox.Show(“File does not exist “); txtFileName.Text = “”; } else{ using (StreamReader sr = new StreamReader(store.OpenFile(txtFileName.Text, FileMode.Open, FileAccess.ReadWrite))) { txtContent.Text = sr.ReadToEnd(); }}}} |
Explanation
- StreamReader is being used to read from the file.
- StreamREader is inside the namespace System.IO.
- We are opening the ISolatedStorageFile for the SilverLight Application.
- We are checking, that if file name provided by user does not exist then displaying the message that file name does not exist.
- We are opening the file in Read Mode and reading the stream into a string.
How to delete file?
private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e){ using (var store = IsolatedStorageFile.GetUserStoreForApplication()){ if (!(store.FileExists(txtFileName.Text))){ MessageBox.Show(“File does not exist “); } else{ store.DeleteFile(txtFileName.Text); MessageBox.Show(“Deleted”);} txtFileName.Text = “”;}} |
Explanation
- We are calling the DeleteFile method on instance of ISolatedStorageFile.
Complete code is as below
MainPage.xaml.cs
using System;using System.Collections.Generic;
using System.Linq; using System.Net; InitializeComponent(); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) private void btnWrite_Click(object sender, System.Windows.RoutedEventArgs e) txtFileName.Text = “”; |
Leave a Reply