Isolated Storage...

Isolated storage is a file storage mechanism without knowing the actual physical location. The objects to utilize this reside in the System.IO.IsolatedStorage namespace.


By: Brian Mains Date: May 15, 2004 Download the code.

Isolated storage is a file storage mechanism without knowing the actual physical location. The objects to utilize this reside in the System.IO.IsolatedStorage namespace, although some of the enumerations needed for isolated storage operations are in the System.IO namespace. This storage area is beneath the user's Application Data folder (see Figure 1), which the folders/files are unique to a user and .NET assembly. However, the security for these files can be an issue. Administrators on the system or highly-trusted assemblies can alter or delete the files stored in information storage. For this reason, be careful what type of data is stored in this structure, and always verify the file’s existence if possible, or set up error handling to catch the errors that will occur if a file is deleted.


Figure 1: Isolated Storage file for a Windows XP Professional, non-roaming profile.

Isolated storage is ideal for applications that have end users who don’t have read/write privileges. Regardless of the security settings, the user can still perform IO operations on files stored in this area. It is also ideal for applications that have to write large amounts of data on the client (can be an alternative for cookies), or for user or custom controls downloaded to a user’s machine that needs to perform IO operations. In addition, users set up with roaming profiles can retain these settings across computers.

Isolated storage works by connecting to the store, or repository, through the IsolatedStorageFile object. This object is not actually a file representation; it actually retrieves the isolated store based on the isolation requirements. The valid isolation parameters are:

These parameters are represented in code through the IsolatedStorageScope enumeration. They specify whether the storage should be isolated by the running assembly, the user context (in ASP.NET applications, this user will be the ASPNET user), the domain, and any roaming profiles if necessary. The combinations shown above must be "Or"ed together, as such: IsolatedStorageScope.Roaming Or IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain. Isolation must be specified using one of the four combinations above, not just by a single one of them (such as User isolation only). Several ways to create an IsolatedStorageFile object are listed below:

objStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly Or IsolatedStorageScope.User, Nothing, Nothing)
objStore = IsolatedStorageFile.GetUserStoreForAssembly
objStore = IsolatedStorageFile.GetUserStoreForDomain

The IsolatedStorageFile's constructor is declared private, so the New keyword cannot be used. Instead, the class has several shared/static functions for creating the object. The first option is to provide the scope of the isolation storage by "Or"ing the isolation level parameters together. Another option is to use the GetUserStoreForAssembly shared/static function, which is the equivalent of declaring isolation by: IsolatedStorageScope.Assembly Or IsolatedStorageScope.User in the GetStore() method. The GetUserStoreForDomain function operates in the same manner, and adds isolation by domain to the list of isolation scopes.

The storage area can contain any number of directories/files. In addition, the IsolatedStorageFile object can create more than one directory in a create statement, by referencing a child directory in the format below. Using "MyAppSettings\SettingsFile" will create both the MyAppSettings directory, and the SettingsFile directory beneath it.

objStore.CreateDirectory("MyAppSettings")
objStore.CreateDirectory("MyAppSettings\SettingsFile")
objStore.DeleteDirectory("MyAppSettings\SettingsFile")

The IsolatedStorageFileStream object is used to access the file. It takes the file path and IsolatedStorageFile object in the constructor, as well as several IO enumerations, such as FileMode, FileAccess, and FileShare. The last two enumerations control the read/write operations on the stream, whereas the first enumeration controls the action to take on the file (open, open or create [if file doesn’t exist], create, append data to the bottom of the file, truncate data, etc.). The open or create option is especially useful if it is unsure that the file will exist when attempting to access it.

'Create the file with default values
objStream = New IsolatedStorageFileStream("MyAppSettings\SettingsFile\settings.txt", _
            FileMode.Create, FileAccess.Write, objStore)
'Open the file for reading
objStream = New IsolatedStorageFileStream("MyAppSettings\SettingsFile\settings.txt", FileMode.Open, _
            FileAccess.Read, objStore)

The IsolatedStorageFileStream object can handle the read/write functionality, but it is easier to assign it to an object that inherits from the System.IO.Stream class. For this example, the StreamReader and StreamWriter objects are used, which will receive the stream as a parameter in the constructor and perform read/write operations to/from the file.

objWriter = New StreamWriter(objStream)
objWriter.WriteLine(ddlLayoutForeColor.SelectedValue)
objWriter.WriteLine(ddlLayoutBackColor.SelectedValue)
objWriter.WriteLine(ddlContentForeColor.SelectedValue)
objWriter.WriteLine(txtApplicationTitle.Text)

objReader = New StreamReader(objStream)
ddlLayoutForeColor.SelectedValue = objReader.ReadLine()
ddlLayoutBackColor.SelectedValue = objReader.ReadLine()
ddlContentForeColor.SelectedValue = objReader.ReadLine()
txtApplicationTitle.Text = objReader.ReadLine()

This is isolated storage at a minimum. A lot more can be accomplished with isolated storage, such as storing XML or DataSet data, serialized objects, etc., but this will get you started into the syntax and structure of isolated storage. In addition, depending on the permissions needed, the IsolatedStorageFilePermission object may need to allocate permissions for the user to write to isolated storage. This object exists in the System.Security.Permissions namespace. This object can also limit the amount of disk space allowed for isolated storage, and adjust it accordingly.

About the Code

The code attached is an example of using a text file to store the look-and-feel settings for a web site. The SiteLayout class will inherit from the System.Web.UI.Page class and will be the site layout for the entire web site in this example. The settings web page alters a text file that the SiteLayout class uses, which affects the rendering of the web site. This is a really simple example of isolated storage for storing user settings.

The code attached is an ASP.NET project. Please copy the folder to your wwwroot directory in order to use this example. Additionally, you may have to set this project up as an application for the virtual directory. In IIS, right-click the IsolatedStorageSite virtual directory, select Properties. Where it says "Application Name", the button next to it should say "Remove". If it doesn't, hit the create button to create the application.

The following files are used in the project, and they both access the isolated storage file:

Depending on your permissions available, an IsolatedStorageFilePermissions object may need to be created to assign permissions to allow the calling user to read/write information to isolated storage.

You may download the code here.