Showing posts with label Isolated Storage. Show all posts
Showing posts with label Isolated Storage. Show all posts

Friday, August 28, 2009

Reading from Isolated Storage

You learned how to write to Isolated Storage in the previous post. Here is how you read from it:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("pref.txt", FileMode.OpenOrCreate, isf);
StreamReader sr = new StreamReader(stream);
SetWidthAndHeight(sr.ReadToEnd());
sr.Close();

Isolated Storage

Isolated Storage - A private file system managed by the .NET Framework.

Why use it?
Isolated Storage provides a safe location, regardless of user access level, to store information that can be isolated by each user on the machine to store such things as user preferences.

How do I use it?
Here's an example where I store the height and width of a window in a text file on closing.
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("pref.txt", FileMode.Create, isf);
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine("Width:" + this.Width);
sw.WriteLine("Height:" + this.Height);
sw.Close();

Where did this text file actually get saved?
Well, on my machine (Vista) it went into C:\Users\[User]\AppData\Local\IsolatedStorage and then through 3 more cryptic folder names until it reached the AssemFiles folder where the "pref.txt" file showed up.

The location can change depending on the OS. See here for other OS locations.

More Info: Article: When and when not to use Isolated Storage