.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

File Handling in Isolated Storage in SilverLight Tutorial

Posted By:Dhananjay Kumar       Posted Date: February 13, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

In This tutorial I will explain, 1. How to create a text file in IsolatedStorageFile of a SilverLight 2 application. 2. How to write into a text file in IsoltaedStorageFile. 3. How to read a text file from IsolatedStorageFile. 4. How to delete a file from IsolatedStorageFile.
 

File Handling in Isolated Storage in Silver Light

Objective:

This article will explain,
  1. How to create a text file in IsolatedStorageFile of a SilverLight 2 application.
  2. How to write into a text file in IsoltaedStorageFile.
  3. How to read a text file from IsolatedStorageFile.
  4. How to delete a file from IsolatedStorageFile.

Please follow, the below steps

Step 1:

Create a SilverLight application. By selecting File->New->Project->SilverLight-> SilverLight Application.

Step 2:

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.

image1.gif

Complete XAML code is as follows.

MainPage.Xaml


  
    
    
      
      
    
    
    
    
    
    
    
    
    
    


So, the output page will look like,

images2.gif

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
  1. This class is inside the namespace System.IO.IsolatedStorage
  2. We could set domain of IsolatedStorageFile either for SilverLight website or for SilverLight Application.
    Both options are depicted in below images.

    images3.gif

    images4.gif

  3. There are many methods exist inside this to work with File operations. For example, create directory, create file, delete directory, delete file etc.

    Below image is depicting all the available methods of IsolatedStorageFile class. Store is instance of this class in below image.

images5.gif

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 = "";
                MessageBox.Show("File Writen");
            }
}

Explanation
  1. StreamWriter is being used to write into the file.
  2. StreamWriter is inside the namespace System.IO.
  3. We are opening the ISolatedStorageFile for the SilverLight Application.
  4. We are checking, that if file name provided by user does not exist then create a new one with the provided name.
  5. 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
  1. StreamReader is being used to read from the file.
  2. StreamReader is inside the namespace System.IO.
  3. We are opening the ISolatedStorageFile for the SilverLight Application.
  4. We are checking, that if file name provided by user does not exist then displaying the message that file name does not exist.
  5. 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
  1. 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;
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 System.IO;
using System.IO.IsolatedStorage;

namespace FileReadingandWritingwithIsolatedStorage
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

         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();
                    }
                }
            }
        }

        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 = "";
                MessageBox.Show("File Writen");
            }
        }
        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 = "";
            }
        }
    }
}

Step 3:

Press F5 to run with debug.

images6.gif

Conclusion:

I have explained in this article, about various file options in isolated storage. Please download ZIP file for better understanding and reference.

Thanks for Reading.

Happy Coding.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend