The .NET CLR provides
isolated storage
to allow the application developer to store data on a
per-user basis. Isolated storage provides much
of the functionality of traditional Windows .ini
files or the more recent HKEY_CURRENT_USER
key in
the Windows Registry.
Applications save data to a unique data
compartment
associated with the application. The CLR
implements the data compartment with a data
store
: typically a directory on the file system.
Administrators are free to limit how much isolated storage individual applications can use. They can also use security so that less trusted code cannot call more highly trusted code to write to isolated storage.
What is important about isolated storage is that the CLR provides a standard place to store your application’s data, but it does not impose (or support) any particular layout or syntax for that data. In short, you can store anything you like in isolated storage.
Typically, you will store text, often in the form of name-value pairs. Isolated storage is a good mechanism for saving user configuration information such as login name, the position of various windows and widgets, and other application-specific, user-specific information. The data is stored in a separate file for each user, but the files can be isolated even further by distinguishing among different aspects of the identity of the code (by assembly or by originating application domain).
Using isolated storage is fairly straightforward. To write to
isolated storage you create an instance of an
IsolatedStorageFileStream
which you initialize
with a filename and a file mode (create, append, etc.):
IsolatedStorageFileStream configFile = new IsolatedStorageFileStream ("Tester.cfg",FileMode.Create);
You then create a StreamWriter
on that file:
StreamWriter writer = new StreamWriter(configFile);
You then write to that stream as you would to any other. Example 21-17 illustrates.
Example 21-17. Writing to isolated storage
namespace Programming_CSharp { using System; using System.IO; using System.IO.IsolatedStorage; public class Tester { public static void Main( ) { Tester app = new Tester( ); app.Run( ); } private void Run( ) { // create the configuration file streamIsolatedStorageFileStream configFile =
new IsolatedStorageFileStream
("Tester.cfg",FileMode.Create);
// create a writer to write to the stream StreamWriter writer = new StreamWriter(configFile); // write some data to the config. file String output; System.DateTime currentTime = System.DateTime.Now; output = "Last access: " + currentTime.ToString( ); writer.WriteLine(output); output = "Last position = 27,35"; writer.WriteLine(output); // flush the buffer and clean up writer.Flush( ); writer.Close( ); configFile.Close( ); } } }
After running this code, search your hard disk for test.cfg. On my machine, this file is found in:
c:Documents and SettingsAdministratorApplicationData MicrosoftCOMPlusIsolatedStorage .4 Url.wj4zpd5ni41dynqxx1uz0x0aoaraftc Url.wj4zpd5ni41dynqxx1uz0ix0aoaraftcfiles
You can read this file with Notepad if what you’ve written is just text:
Last access: 5/2/2001 10:00:57 AM Last position = 27,35
Or, you can access this data programmatically. To do so, reopen the file:
IsolatedStorageFileStream configFile = new IsolatedStorageFileStream ("Tester.cfg",FileMode.Open);
Create a StreamReader
object:
StreamReader reader = new StreamReader(configFile);
and use the standard stream idiom to read through the file:
string theEntry; do { theEntry = reader.ReadLine( ); Console.WriteLine(theEntry); } while (theEntry != null); Console.WriteLine(theEntry);
Example 21-18 provides the entire source needed to read the file.
Example 21-18. Reading from isolated storage
namespace Programming_CSharp
{
using System;
using System.IO;
using System.IO.IsolatedStorage;
public class Tester
{
public static void Main( )
{
Tester app = new Tester( );
app.Run( );
}
private void Run( )
{
// open the configuration file stream
IsolatedStorageFileStream configFile =
new IsolatedStorageFileStream
("Tester.cfg",FileMode.Open);
// create a standard stream reader
StreamReader reader =
new StreamReader(configFile);
// read through the file and display
string theEntry;
do
{
theEntry = reader.ReadLine( );
Console.WriteLine(theEntry);
} while (theEntry != null);
reader.Close( );
configFile.Close( );
}
}
}
Output:
Last access: 5/2/2001 10:00:57 AM
Last position = 27,35
3.141.27.74