IsolatedStorage to the Rescue

August 18, 2007

no comments

Up until recently I’ve been totally unaware to an extremely useful .NET feature known as IsolatedStorage. It goes like this. Let’s say you have a client application, right? And that application needs to store some data, right? Some user preferences maybe. Thing is, trying to simply write that data into a file can be a problem. Your application might not have the permissions needed to write to the file system. In fact, if we’re talking about a browser application, that’s a very likely situation.

Here IsolatedStorage comes to our rescue. It allows you to write application or assembly specific data to a designated isolated place in the file system. This way, the user is safe from malicious code and your application can persist some data.

So how do you use it? Like any other file stream. You have to import the System.IO.IsolatedStorage namespace, and write something like this:

IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FileName, FileMode.Create)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine("Hello World"); } }

The first line determines which kind of isolated storage are you interested in. You can read more about that here.

I’ve created a small helper that will allow me to simplify my code that uses IsolatedStorage into something like this:

private void SaveDirectoryPath(string path) { IsolatedStorageHelper.Write(writer => writer.WriteLine(path)); } private string LoadDirectoryPath() { string path = IsolatedStorageHelper.Read<string>(reader => reader.ReadLine()); return path; }

Basically, I’m using IsolatedStorageHelper encapsulates opening and closing the IsolatedStorageStream, and allows you to focus on just writing\reading your data (read this if you’re unfamiliar with the lambda “=>” syntax).

Here’s the full source for IsolatedStorageHelper (You can also download the source from here):

public delegate void WriteCallback(StreamWriter writer); public delegate TResult ReadCallback<TResult>(StreamReader reader); public class IsolatedStorageHelper { private const string FileName = "appSettings"; public static void Write(WriteCallback callback) { IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FileName, FileMode.Create)) { using (StreamWriter writer = new StreamWriter(stream)) { callback(writer); } } } public static TResult Read<TResult>(ReadCallback<TResult> callback) { IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate)) { using (StreamReader reader = new StreamReader(stream)) { return callback(reader); } } }

Oh, and if you were wondering where the IsolatedStorage is stored, it’s somewhere in the Documents and Settings folder. Exact locations can be found here.

And remember, using IsolatedStorage to store application or user specific data is a best practice. Your application shouldn’t require any privileges that it doesn’t really need.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*