Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET
Article

Isolated Storage in .NET to store application data

Rate me:
Please Sign up or sign in to vote.
2.78/5 (32 votes)
26 Mar 20041 min read 163.6K   40   15
This sample demonstrates using the Isolated Storage to save and retrieve application data in .NET applications.

Introduction

Many programmers use the config file to keep the application configuration data. But one disadvantage with this is, it is a read only mechanism. You cannot update data in application config files, using the ConfigurationSettings classes in .NET. In earlier days, .ini files or registry was used to save application specific data.

Of course, we can use regular files to save application data. But now the question is how to protect the data, where to save it and all other mess!

Isolated Storage

.NET introduces a concept called Isolated Storage. Isolated Storage is a kind of virtual folder. Users never need to know where exactly the file is stored. All you do is, tell the .NET framework to store your file in Isolated Storage. The physical location of Isolated Storage varies for each Operating System. But your application simply uses the .NET classes to create and access files, without bothering where it is physically located. And you can have Isolated Storage specific to each Assembly or each Windows user.

The following code sample demonstrates the basic create, write and read operations with Isolated Storage.

Make sure you include the following directives on top of your file:

C#
using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;

Code sample:

C#
const string ISOLATED_FILE_NAME = "MyIsolatedFile.txt";

//-------------------------------------------------------

// Check if the file already exists in isolated storage.

//-------------------------------------------------------

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

string[] fileNames = isoStore.GetFileNames( ISOLATED_FILE_NAME );

foreach ( string file in fileNames )
{
    if ( file == ISOLATED_FILE_NAME )
    {
       Debug.WriteLine("The file already exists!");
    }
}

//-------------------------------------------------------

// Write some text into the file in isolated storage.

//-------------------------------------------------------

IsolatedStorageFileStream oStream = 
  new IsolatedStorageFileStream( ISOLATED_FILE_NAME, 
  FileMode.Create, isoStore );

StreamWriter writer = new StreamWriter( oStream );
writer.WriteLine( "This is my first line in the isolated storage file." );
writer.WriteLine( "This is second line." );
writer.Close();

//-------------------------------------------------------

// Read all lines from the file in isolated storage.

//-------------------------------------------------------

IsolatedStorageFileStream iStream = 
  new IsolatedStorageFileStream( ISOLATED_FILE_NAME, 
  FileMode.Open, isoStore );

StreamReader reader = new StreamReader( iStream );
String line;

while ( (line = reader.ReadLine()) != null )
{
    Debug.WriteLine( line );
}

reader.Close();

Visit here to read my C# Tutorial collection.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Tony is a seasoned .NET developer who recently switched his focus to Windows 8 and SEO. You may visit his technology websites: www.dotnetspider.com and www.techulator.com.

Comments and Discussions

 
GeneralMy vote of 5 Pin
YPD26-Dec-11 1:37
YPD26-Dec-11 1:37 
GeneralMy vote of 1 Pin
Petoj8720-Dec-09 9:18
Petoj8720-Dec-09 9:18 
GeneralRe: My vote of 1 Pin
Member 803644630-Jan-12 8:29
Member 803644630-Jan-12 8:29 
GeneralCorrect me if im wrong Pin
Petoj8714-Dec-09 11:57
Petoj8714-Dec-09 11:57 
GeneralRe: Correct me if im wrong Pin
Thesisus7-Jun-11 10:27
Thesisus7-Jun-11 10:27 
Answerwriting to config files Pin
gandalf_19-Mar-09 9:18
gandalf_19-Mar-09 9:18 
GeneralMy vote of 1 Pin
DZaK8328-Jan-09 12:46
DZaK8328-Jan-09 12:46 
GeneralRe: My vote of 1 Pin
Jark8-Oct-09 23:04
Jark8-Oct-09 23:04 
Question.config files Pin
hfrmobile25-Jun-08 1:33
hfrmobile25-Jun-08 1:33 
QuestionCan I use IsolatedStorage with asp.net? Pin
Member 42592871-Feb-08 3:47
Member 42592871-Feb-08 3:47 
QuestionHow do I locate the file in the file system? Pin
Grey|Pixels25-Jan-07 6:05
Grey|Pixels25-Jan-07 6:05 
AnswerRe: How do I locate the file in the file system? [modified] Pin
hfrmobile25-Jun-08 1:28
hfrmobile25-Jun-08 1:28 
GeneralRe: How do I locate the file in the file system? Pin
MSaty11-Aug-08 0:25
MSaty11-Aug-08 0:25 
GeneralRe: How do I locate the file in the file system? Pin
hfrmobile11-Aug-08 1:05
hfrmobile11-Aug-08 1:05 
GeneralRe: How do I locate the file in the file system? Pin
cerberiukas21-Dec-08 21:36
cerberiukas21-Dec-08 21:36 

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

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