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();
|
| Author: Pat Tormey 26 Sep 2004 | Member 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 2004 | Member 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 2004 | Member 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
|