Click here to Skip to main content
Click here to Skip to main content

Use of Isolated Storage (A Simple Demo)

, 28 Jul 2008
Rate this:
Please Sign up or sign in to vote.
The article will help you to use Isolated Storage Classes

Introduction

Isolated Storage Namespace that is included in .NET Class Library is a good feature introduced in Visual Studio 2003 or later to hide certain data from the outside. The file where it actually stores those data is somewhat abstract to the user and also based on its scope specified while creating the application. In this article, I am going to discuss the basics of how to use Isolated Storage Classes and specify scope to those. The data stored through Isolated Storage is not accessible to Less trusted Applications, as they are scoped within the Assembly it produces.

The classes that we need in this project are listed below: 

  • IsolatedStorageFile: This is the Primary FileStream Object. It is the main class that manages directory, files and gets store for the assembly.
  • IsolatedStorageFileStream: Exposes a file within the Isolated file Location. It is the main File Stream object used for Reading or Writing on a File.
  • IsolatedStorageException: It is the exception object which is thrown when any Runtime exception is generated during Isolated file Operation.

We need to include System.IO.IsolatedStorage as well. Please note: The IsolatedStorage Classes are included in mscorlib.dll, which is automatically included. So no Reference is required.

Background

It is a very common practice to use Files to store sensitive information. Information like Web Service Path, User Settings, Application Settings are stored into Registry, config files or in some text files. The problem using them is that it does not belong to the Assembly itself, and thus should be exported whenever the assembly is exported.

Using Isolated Storage, we can include those data within the assembly itself, which will be automatically transmitted with that assembly. We may also write information to Application Data Blocks, System Files for better security. Use of Isolated Storage eliminates any Com unsecure applications to access that sensitive information.

Using the Code

This is a basic example of how we can write or fetch from an Isolated Storage. To do this, we need an object of IsolatedStorageFile. This class has a static function called GetStore. We can specify what type of Store we need.

Here is the list of all scopes available :

Members Name Description of the Use
None No Isolated Storage Usage
User Isolated Storage scoped to User Identity
Domain Isolated Storage Scoped to Application Domain Identity
Assembly Isolated Storage Scoped to Identity of the Assembly
Roaming The Isolated Storage Can roam.
Machine Isolated Storage Scoped to the Machine
Application Isolated Storage Scoped to the Application

Generally we use Domain as Application Scope, which identifies the Application Domain and stores data within the Application Domain.

Now it is time to understand the code.

As far as the basics are concerned, see the code below:

//Main Class that is used for Isolated Storage
using System;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

namespace IsolatedStorage
{
    public partial class ISF : Form
    {
        private string filename;
        private string directoryname;
        IsolatedStorageFile isf = null;

        /// <summary>
        /// Constructor for ISF, Used generally for Initialization 
        /// </summary>
        public ISF()
        {
            filename = string.Empty;
            directoryname = string.Empty;
            InitializeComponent();
            isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|
        IsolatedStorageScope.Assembly|IsolatedStorageScope.Domain, 
        typeof(System.Security.Policy.Url),typeof(System.Security.Policy.Url));
        }

        /// <summary>
        /// Invoked when Button1 is Clicked
        /// </summary>
        /// <param name="sender">Object that invokes the Event 
        /// ( Here it is Button1)</param>
        /// <param name="e">Event Related Arguments</param>
        private void button1_Click(object sender, EventArgs e)
        {
            LoadInfo();
        }

        /// <summary>
        /// Clear out Existing Loaded Information from the List Boxes
        /// </summary>
        private void LoadInfo()
        {
            lstDirectories.Items.Clear();
            lstFileList.Items.Clear();
            lstFileList.Items.AddRange(isf.GetFileNames("*"));
            lstDirectories.Items.AddRange(isf.GetDirectoryNames("*"));
        }

        /// <summary>
        /// Enabling btnSave when Text is Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
        }

        /// <summary>
        /// Called when Save button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IsolatedStorageFileStream isfs;
                isfs = new IsolatedStorageFileStream(Path.Combine(getDirectoryName(), 
                        getFileName()), FileMode.Create, isf);
                byte[] data = Encoding.GetEncoding("utf-8").GetBytes(textBox1.Text);
                isfs.Write(data, 0, data.Length);
                isfs.Close();
                LoadInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Runtime Error:" + ex.Message);
            }
        }

        /// <summary>
        /// Get Directory Name from the user of Session Context
        /// </summary>
        /// <returns></returns>
        private string getDirectoryName()
        {
            if (directoryname == string.Empty)
            {
                frmNewName dialog = new frmNewName();
                dialog.Text = "Enter Directory Name:";
                if (dialog.ShowDialog() == DialogResult.OK)
                    return dialog.Path;
                else
                    return string.Empty;

            }
            else
                return directoryname; 
        }

        /// <summary>
        /// Gets FileName from User or Current Session
        /// </summary>
        /// <returns></returns>
        private string getFileName()
        {
            if (filename == string.Empty)
            {
                frmNewName dialog = new frmNewName();
                dialog.Text = "Enter File Name:";
                if (dialog.ShowDialog() == DialogResult.OK)
                    return dialog.Path;
                else
                    return string.Empty;
            }
            else
                return filename;
        }

        /// <summary>
        /// Selected Index changed for File. Loads the File to the TextBox also
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstFileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                filename = lstFileList.SelectedItem.ToString();
                IsolatedStorageFileStream isfs = new IsolatedStorageFileStream
            (Path.Combine(directoryname,filename), FileMode.Open, isf);
                StreamReader sr = new StreamReader(isfs);
                textBox1.Text = sr.ReadToEnd();
                sr.Close();
                isfs.Close();
                btnSave.Enabled = false;
                lblItemStat.Text = Path.Combine(directoryname, filename);
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Runtime Error:" + ex.Message);
            }
        }

        /// <summary>
        /// Creates New Directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNewDir_Click(object sender, EventArgs e)
        {
            directoryname = string.Empty;
            directoryname = getDirectoryName();
            isf.CreateDirectory(directoryname);
            LoadInfo();
        }
        
        private void btnFile_Click(object sender, EventArgs e)
        {
            filename = string.Empty;
            filename = getFileName();
        }

        private void lstDirectories_SelectedIndexChanged(object sender, EventArgs e)
        {
            directoryname = lstDirectories.SelectedItem.ToString();  
            lstFileList.Items.Clear();
            lstFileList.Items.AddRange
		(isf.GetFileNames(Path.Combine(directoryname,"*")));
        }

        /// <summary>
        /// Removes currently selected Directory. Note It will 
        /// throw error if Directory is not Empty
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveDir_Click(object sender, EventArgs e)
        {
            try
            {
                isf.DeleteDirectory(lstDirectories.SelectedItem.ToString());
                directoryname = string.Empty;
                LoadInfo();
            }
            catch (IsolatedStorageException ex)
            {
                MessageBox.Show("Runtime Error:" + ex.Message);
            }

        }
        /// <summary>
        /// Removes Chosen File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveFile_Click(object sender, EventArgs e)
        {
            try
            {
                isf.DeleteFile(Path.Combine(directoryname,
                    lstFileList.SelectedItem.ToString()));
                filename = string.Empty;
            }
            catch (IsolatedStorageException ex)
            {
                MessageBox.Show("Runtime Error:" + ex.Message);
            }
        }
    }
}        

This code might look a bit verbose to you. Ok, let us define it shortly.

To use Isolated Storage, we need to create a store for the data based on the scope we want. To get the store, we will write:

<>IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User|
        IsolatedStorageScope.Assembly|IsolatedStorageScope.Domain, null,null);>

In the above line, I have created an object of IsolatedStorage and got the store from Current User Domain.

After we get the Store, we need IsolatedStorageFileStream to read or write on a particular file.

IsolatedStorageFileStream isfs = new IsolatedStorageFileStream
                    ("abc.txt",FileMode.Create, isf);

In the above line, I have created on IsolatedStorageFileStream, which will create "abc.txt" file if it is not existing.

After doing so, we need StreamReader to read or StreamWriter to write on the file.

For reading from the file, we have to use FileMode.Open to ensure that it gives filenotfound error if the file is not found.

//For Reading

 StreamReader sr = new StreamReader(isfs);
 string fileData = sr.ReadToEnd();//Reads from the File it is pointed to

// For Writing

 StreamWriter sw = new StreamWriter(isfs);
 sw.WriteLine(fileData); // Writes Data to the File Opened

If the IO operation fails, it may throw IsolatedStorageException.

That's all we need. The others in the code are just to present you a good piece of interface.

Snapshots

coolImage1.JPG

In the above figure, it shows a button that invokes the Directory and Files to be loaded. The Button GetList will list all the directory and the Files stored in the current scope.

We may change scope to get other stores too.

coolimage2.JPG

The save button will save the file written on the Textbox to the chosen file. If no file is chosen yet, the file choose dialog box will be displayed.

coolImage3.JPG

The 3rd picture shows the dialog box that comes when the filename does not exist.

Try running the application and check. There may be some bugs on the application. I will fix them very soon.

Points of Interest

It's really fun to hide your important files from the user by including the feature of Isolated Storage classes. I think you all would have fun creating an application using this feature provided with .NET.

Functional Matrix

Instead of using GetStore function, you may also use the Custom functions provided with IsolatedStorageFile class. Here is the equivalence matrix:

IsolatedStorageFile.GetMachineStoreForApplication()

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Application | 
	IsolatedStorageScope.Machine, null, null);
IsolatedStorageFile.GetMachineStoreForAssembly()

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
IsolatedStorageScope.Machine, null, null);IsolatedStorageFile.GetMachineStoreForDomain()

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
	IsolatedStorageScope.Domain | IsolatedStorageScope.Machine, null, null);
IsolatedStorageFile.GetUserStoreForApplication() 

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Application | 
	IsolatedStorageScope.User, null); 
IsolatedStorageFile.GetUserStoreForAssembly()

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
	IsolatedStorageScope.User, null, null);
IsolatedStorageFile.GetUserStoreForDomain()

is equivalent to:

IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
	IsolatedStorageScope.Domain | IsolatedStorageScope.User, null, null);

You can use any of these functions to get its store.

History

This is the first version of the article. I would like to update the application later to make it more handy.

First Version:  IsolatedStorage.zip - This is the first version of the Application. It may contain some bugs. I will fix them in future revisions. My intension is to give basic knowledge about the use of the system.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur
Architect
India India
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionDrive letter Pinmemberaktivo16-Apr-14 7:32 
QuestionCan I set a custom path to Isolated Store? PinmemberShiv...9-May-13 3:42 
QuestionAny option to export the list ? PinmemberPraveen Kullu24-Jul-12 5:07 
GeneralMy vote of 5 PinmemberAll Time Programming29-Mar-12 1:48 
GeneralMy vote of 5 Pinmembero1010wanabe25-Apr-11 4:58 
GeneralMy vote of 5 PinmemberGPUToaster11-Aug-10 23:39 
GeneralNice article Pinmemberranjan_namitaputra2-Oct-09 11:13 
GeneralRe: Nice article PinmemberAbhishek Sur3-Oct-09 6:40 
GeneralGet the location of the isolated storage. PinmemberDennis Dam27-May-09 4:30 
GeneralRe: Get the location of the isolated storage. PinmemberAbhishek Sur28-May-09 22:08 
GeneralVC with file text Pinmemberthanhnienlom18-Feb-09 1:58 
RantRe: VC with file text Pinmemberwilliamnw18-Feb-09 3:08 
GeneralClear isolated storage PinmemberAndrusM6-Aug-08 1:35 
GeneralRe: Clear isolated storage PinmemberAbhishek sur6-Aug-08 21:35 
GeneralRe: Clear isolated storage PinmemberAndrusM6-Aug-08 23:59 
GeneralRe: Clear isolated storage PinmemberAbhishek sur11-Aug-08 4:01 
I think you dong have permission to remove UserAssembly Information...
 
Try GETSTORE with that.. .
If it still not allowing ..
use
IsolatedStorageFilePermission (PermissionState.None)
 
I think that will do
 
Abhishek Sur

Web Developer


Generalfile protection PinmemberMSaty29-Jul-08 23:16 
GeneralRe: file protection PinmemberAbhishek sur30-Jul-08 4:24 
QuestionRe: file protection PinmemberMSaty10-Aug-08 23:06 
GeneralRe: file protection PinmemberMSaty11-Aug-08 0:28 
GeneralRe: file protection PinmemberAbhishek sur11-Aug-08 4:13 
GeneralRe: file protection PinmemberAbhishek sur11-Aug-08 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 28 Jul 2008
Article Copyright 2008 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid