[ Team LiB ] Previous Section Next Section

21.7 Isolated Storage

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, which is typically a directory on the filesystem.

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, 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);

Then create a StreamWriter on that file:

StreamWriter writer = 
    new StreamWriter(configFile);

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 stream
            IsolatedStorageFileStream 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 Tester.cfg. On my machine, this file is found in:

c:\Documents and Settings\Administrator\ApplicationData\
Microsoft\COMPlus\IsolatedStorage\0.4\
Url.wj4zpd5ni41dynqxx1uz0x0aoaraftc\
Url.wj4zpd5ni41dynqxx1uz0ix0aoaraftc\files

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);

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);

Isolated storage is scoped by assembly (so, if you shut down your program and start it later, you can read the configuration file you created). Example 21-18 provides the method needed to read the file. Replace the Run( ) method in the previous example, recompile it, and run it (but don't change its name, or it won't be able to access the isolated storage you created previously):

Example 21-18. Reading from isolated storage
        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

    [ Team LiB ] Previous Section Next Section