C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Isolated Storage in .Net to store application data


Posted Date: 04 Feb 2004    Resource Type: Articles    Category: .NET Framework

Posted By: Tony John       Member Level: Gold
Rating:     Points: 10



The resource has not been reviewed by Editors yet. Readers are adviced to use their best judgement before accessing this resource.
This resource will be reviewed shortly.
If you think this resource contain inappropriate content, please report to webmaster.


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.

Ofcourse 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!

.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, telling 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();




Responses

Author: Pat Tormey    26 Sep 2004Member Level: Bronze   Points : 0
First thanks for the help with Isolated Storage
It seems that Isolated Storage would need locks to wite the file and maybe deletes could fail due to a file in use.

What expections should I be watching for? How about lcoks?

Thanks
Pat
New Hampshire USA


Author: madhusudhana    23 Nov 2004Member Level: Bronze   Points : 0
Hi tony,

Thanks for the good information about Isolated Storage. But we should also need to take care of Parallel Access and is there any chance to delete the file after using it.

Hope u understand the problem.

ThanQ
Madhusudhana


Author: Debasish Bose    01 Dec 2004Member Level: Bronze   Points : 0
Using isolated storage one can build a trial licensing framework

But the CLR security policy must be checked to access the proper scope of the IS. If there is too high security configured catch a security exception to inform the client app to do the necessary

There is also no mention about the .NET SDK tool called storedadm.exe which can ve invoked from the VS.Net command prompt.

To remove IS files use :
storeadm /remove

To list all the contents scoped by current user

storeadm /list




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: Working with HashTable
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



Read TATA Nano reviews.
dotNet Slackers   BizTalk Adaptors    Web Design   Scripts
Are you waiting for engineering entrance result? Watch here for engineering entrance results.
fax server

Contact Us    Privacy Policy    Terms Of Use