Isolated Storage

This refers to a location where you can keep application configurations information.
In a nutshell, isolated storage is a location on your drive that only a specific application can find and use.

Using Isolated Storage

The first step in using isolated storage is to open the store. You use the IsolatedStorageFile class for this.
Despite its name, this class does not represent a file but the isolated store itself. Here's the code:

Dim store As IsolatedStorageFile 
store = IsolatedStorageFile.GetUserStoreForApplication()

The method GetUserStoreForApplication() obtains storage that is scoped for both the user and application.
Other methods are available for obtaining storage that is scoped differently.

Once you have your store you treat it essentially as a directory, or folder.
The framework provides the IsolatedStorageFileStream class to work with files in the store.
This class extends the FileStream class and can be used in almost all situations where you could use a FileStream, such as to create a StreamReader or StreamWriter.

Let's look at an example. Assume that store represents your isolated storage, as created in the code snippet above, and that myDataSet represents a DataSet that you want to serialize:

Dim stream as new IsolatedStorageFileStream("MyData.xml", FileMode.Create, store) 

For other kinds of data, you would use a StreamWriter, as in this snippet that writes text data to a file in isolated storage:

Dim stream as new IsolatedStorageFileStream("MyData.txt", FileMode.Create, store) 
Dim sw As StreamWriter = new StreamWriter(stream)
sw.WriteLine("Line 1")
sw.WriteLine("Line 2")

Note the use of the store's Dispose() method. Calling this method releases all resources associated with the store and should be done before releasing all references to the store.
Call the Close() method when you are finished using the store.

Creating directories within the isolated store is also done in the same manner as with the regular file system:


Reading from Isolated Storage

Reading data from files that are in isolated storage follows the same logic.
You create an IsolatedStorageFileStream and then use it as you would a regular stream for reading data.
Of course, the store that you open for reading must have an appropriate scope for reading the files.

What Scopes are available ?

I have alluded to the various scopes that are available for isolated storage. Isolated storage always has user scope, meaning that it is restricted to the user who created it.
You can combine several other scopes with user scope.
Two of these other scopes are straightforward
Application scope: Storage is scoped to the identity of the application.
Machine scope: Storage is scoped to the identity of the machine.

Three other scopes may require a bit of explanation. Assembly scope is tied to the identity of a particular assembly.
This scope is useful when two or more applications use the same assembly.
You can use this scope to create isolated storage that will be accessible to all of these applications (subject to user scope restrictions, of course).

Domain scope is based on the evidence of the application. In managed code, the word evidence has specific meanings.
When used to apply to isolated storage scope, domain is typically the full URL for a Web application and the path to the EXE file for a desktop application. The use of domain scope is shown in the figure.

Roaming scope is used for isolated stores that moves with a user who has a roaming user profile.
The storage files and written to a network location and then downloaded to any computer the user logs on from.

Disadvantages with Isolated Storage

Isolated storage does not provide secure storage. Unmanaged code can access the files, and the administrator can open or delete the files.
Isolated storage can impose significant disk space overhead if it's used for settings that are the same for multiple users. It is not well-suited for storing large amounts of data to be shared between users.
Administrators can set isolated storage quotas, so you cannot count on having any particular amount of isolated storage available. Because of this, error-handling is essential in code that writes to isolated storage.
To use isolated storage, code must have IsolatedStorageFilePermission. This is not often an issue, but can be a problem if a machine is drastically locked down by administrative security policies.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext