Click here to Skip to main content
6,292,426 members and growing! (10,222 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Isolated Storage in .NET to store application data

By T Manjaly

This sample demonstrates using the Isolated Storage to save and retrieve application data in .NET applications.
C#, VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, ASP.NET, Visual Studio, Dev
Posted:26 Mar 2004
Views:55,331
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
26 votes for this article.
Popularity: 3.79 Rating: 2.68 out of 5
10 votes, 38.5%
1
4 votes, 15.4%
2
3 votes, 11.5%
3
3 votes, 11.5%
4
6 votes, 23.1%
5

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:

using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;

Code sample:

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

About the Author

T Manjaly


Member


See my C# Tutorials for beginners and offshore software development at www.dotnetspider.com

Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
Answerwriting to config files Pinmembergandalf12310:18 19 Mar '09  
GeneralMy vote of 1 PinmemberDZaK8313:46 28 Jan '09  
Question.config files Pinmemberrubicon_hfr2:33 25 Jun '08  
GeneralCan I use IsolatedStorage with asp.net? PinmemberMember 42592874:47 1 Feb '08  
GeneralHow do I locate the file in the file system? PinmemberGrey|Pixels7:05 25 Jan '07  
AnswerRe: How do I locate the file in the file system? [modified] Pinmemberrubicon_hfr2:28 25 Jun '08  
GeneralRe: How do I locate the file in the file system? PinmemberMSaty1:25 11 Aug '08  
GeneralRe: How do I locate the file in the file system? Pinmemberhfrmobile2:05 11 Aug '08  
GeneralRe: How do I locate the file in the file system? Pinmembercerberiukas22:36 21 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Mar 2004
Editor: Smitha Vijayan
Copyright 2004 by T Manjaly
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project