Tuesday, August 5, 2008

Isolated Storage

Isolated Storage

When an application stores data in a file, the file name and storage location must be carefully chosen to minimize the possibility that the storage location will be known to another application and, therefore, vulnerable to corruption. Without a standard system in place to manage these problems, developing ad hoc techniques that minimize storage conflicts can be complex and the results can be unreliable.

With isolated storage, data is always isolated by user and by assembly. Credentials such as the origin or the strong name of the assembly determine assembly identity. Data can also be isolated by application domain, using similar credentials.

Administrators can limit how much isolated storage an application or a user has available, based on an appropriate trust level. In addition, administrators can remove all a user's persisted data. To create or access isolated storage, code must be granted the appropriate IsolatedStorageFilePermission.

System.IO.IsolatedStorage Namespace

The System.IO.IsolatedStorage namespace contains types that allow the creation and use of isolated stores. With these stores, you can read and write data that less trusted code cannot access and prevent the exposure of sensitive information that can be saved elsewhere on the file system. Data is stored in compartments that are isolated by the current user and by the assembly in which the code exists. Additionally, data can be isolated by domain. Roaming profiles can be used in conjunction with isolated storage so isolated stores will travel with the user's profile. The IsolatedStorageScope enumeration indicates different types of isolation. For more information about when to use isolated storage, see Performing Isolated Storage Tasks.

The IsolatedStorageFile class provides most of the necessary functionality for isolated storage. Use this class to obtain, delete and manage isolated storage. The IsolatedStorageFileStream class handles reading and writing files to a store. This is similar to reading and writing in standard File I/O classes. For more information about I/O, see the System.IO namespace.

Writing and reading – IsolatedStorage

Write

public void StoreDataIsolated(string strFilename, string strStoreValue)

{

using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStream isoStream =

new IsolatedStorageFileStream(strFilename, FileMode.Create, isoFile))

{

using (StreamWriter sw = new StreamWriter(isoStream))

{

sw.Write(strStoreValue);

}

}

}

}

Read

public string ReadDataIsolated(string strFilename)

{

string strRetValue = "";

using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStream isoStream =

new IsolatedStorageFileStream(strFilename, FileMode.OpenOrCreate, isoFile))

{

using (StreamReader sw = new StreamReader(isoStream))

{

strRetValue = sw.ReadToEnd();

}

}

}

return strRetValue;

}