Isolated Storage

Isolated storage allows applications to store data on a per-user basis in a safe and consistent way. The .NET Framework uniquely identifies applications or assemblies and uses this identity to create a directory where the application can safely write data.

Caution

Isolated storage is not suitable for storing secure or confidential data. The .NET Framework enforces isolation for managed code, but unmanaged code and code with high system trust are able to access the files in the store.

The isolated storage mechanism is the cross-platform equivalent of storing data in the Windows registry. The programmer doesn’t have to be aware of the identity of the current user or the details of where application data will be stored. This section explores the System.IO.IsolatedStorage namespace and explains how the .NET Framework can help the programmer isolate data easily.

Obtaining and Managing the Isolated Stores

At the heart of this isolated stores mechanism is the misleadingly named IsolatedStorageFile class. Rather than representing a file, this class represents the storage area that contains files and directories. This class can be used to manage the contents of the storage area and to obtain different levels of isolation.

The static factory method IsolatedStorageFile.GetStore is used to obtain instances of the IsolatedStorageFile class. As a parameter, this method takes a bit mask, which defines the level of isolation required. Valid values are members of the IsolatedStorageScope enumeration. These include the values described in Table 10-11.

Table 10-11. Valid Values in the IsolatedStorageScope Enumeration

Isolation Level

Description

IsolatedStorageScope.Assembly

Isolation is scoped to the identity of the assembly. If multiple applications share the same assembly, all applications will be able to access the data in the isolated store.

IsolatedStorageScope.Domain

Isolation is scoped to the identity of the application domain. Multiple applications that share the same assembly will each access different isolated stores.

IsolatedStorageScope.None

No isolated storage. Currently not used.

IsolatedStorageScope.Roaming

The isolated storage can be placed in a location that will be available to roaming users.

IsolatedStorageScope.User

Isolation is scoped by the identity of the user.

The permutations accepted by GetStore are as follows:

  • User|Assembly

  • User|Assembly|Domain

  • Roaming|User|Assembly

  • Roaming|User|Assembly|Domain

In practice, it’s much easier to obtain an IsolatedStorageFile instance by using one of the two static factory methods included in the class than to use the GetStore method. The following statements demonstrate how to easily obtain stores isolated by assembly and by application domain:

// Obtain store isolated by assembly
IsolatedStorageFile x_assembly_iso =
    IsolatedStorageFile.GetUserStoreForAssembly();

// Obtain store isolated by application domain
IsolatedStorageFile x_domain_iso   =
    IsolatedStorageFile.GetUserStoreForDomain();

Once obtained, IsolatedStorageFile can be used to manage the contents of the store, and the programmer can create and delete directories, delete files, and enumerate the contents. Creating files and obtaining content from them is discussed in the next section. The members of the IsolatedStorageFile class are listed in Table 10-12.

Table 10-12. The IsolatedStorageFile .NET Class

Member

Description

AssemblyIdentity

Gets an assembly identity that is used to set the scope for isolation.

DomainIdentity

Gets an application domain identity that is used to set the scope for isolation.

CurrentSize

The number of bytes of data in the store. Administrators can define quotas for storage.

MaximumSize

The maximum number of bytes that can be written to the store.

Scope

The scope of the store.

Close()

Closes a store.

CreateDirectory()

Creates a new directory within the store.

DeleteDirectory()

Deletes a directory within the store.

DeleteFile()

Deletes a file from the store.

GetDirectoryNames()

Enumerates the directories in the store that match a specified search pattern.

GetFileNames()

Enumerates the files in the store that match a specified search pattern.

GetStore()

Obtains an IsolatedStorageFile based on the specified scope.

GetUserStoreForAssembly()

Obtains an IsolatedStorageFile that is isolated by assembly.

GetUserStoreForDomain()

Obtains an IsolatedStorageFile that is isolated by application domain.

Remove()

Removes the storage area and all contents.

Reading and Writing Isolated Data

The IsolatedStorageFileStream class is used to read and write isolated data. This class is derived from System.IO.FileStream and is the only mechanism available to stream isolated data. With the exception of the constructors, the members of this class are the same as those of FileStream. Where the constructor signature matches one of the FileStream members, the specified file will be opened in a store that is isolated by application domain.

Additional constructors are provided that allow the programmer to specify a store (which may have a different level of isolation) obtained through the IsolatedStorageFile.GetStore method or one of the factory methods in the same class.

All path names used in the constructor are relative to the root of the isolated storage area. The following example demonstrates using the IsolatedStorageFileStream class to create a new file, write out some simple data, and then read it back in. Because the IsolatedStorageFileStream inherits from FileStream, asynchronous I/O is supported and reader and writer classes can be used for synchronous operations.

Executing these statements leads to the string "C# for Java Developers" being displayed on the console. However, we also have an isolated storage area that contains a single file, named sample.txt. Because we didn’t specify an isolation store to use in the stream constructor, the default will ensure that the data is isolated for the application domain.

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

public class IsolatedDemo {

public IsolatedDemo() {
        // create the file
        IsolatedStorageFileStream x_stream =
            new IsolatedStorageFileStream("sample.txt",
            FileMode.Create);
        StreamWriter x_writer = new StreamWriter(x_stream);
        // write the data
        x_writer.WriteLine("C# for Java Developers");
        x_writer.Close();

        // read the data back in
        x_stream = new IsolatedStorageFileStream("sample.txt",
            FileMode.Open);
        StreamReader x_reader = new StreamReader(x_stream);
        Console.WriteLine(x_reader.ReadLine());
        x_reader.Close();
    }
    }

Isolated Storage Summary

Isolated storage is a simple mechanism that allows programmers to store data on a per-user basis. Where possible, users should use isolated storage in preference to using the Windows registry or attempting to manage a directory hierarchy manually. While not suitable for storing secure or confidential data, the isolated storage mechanism is incredibly useful and allows assemblies that have low security requirements to store data persistently.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset