Oude artikelen van mijzelf: Isolated Storage

C

In mijn 404 logs kom ik nog af en toe hits tegen van mensen op zoek naar hele oude berichten… Dankzij het internet archief heb ik een groot aantal terug gevonden… Hierbij eentje over Isolated Storage in .Net 1.1…

IsolatedStorage

IsolatedStorage, the storage location for Window.Forms applications.

IsolatedStorage

Last week I’ve developed a small game (for personal use). While developing this game (the all time favorite “Yatzhee”) I wanted to store highscores. But (since I don’t have much experience with Window Forms development) I had no idea were to store the data. I wanted to keep the game simple, and wanted it to run everywhere.

Storing the data in the filesystem seemed like the best option, but I ran across an issue with user rights. I will explain the advantages of the IsolatedStorage in this rather short article.

While searching for alternatives I ran across the IsolatedStorage (located in the System.IO namespace). The IsolatedStorage is a save area within the FileSystem were an application can freely store data. This location is protected from other applications and specific for the application and user.

The advantage of the IsolatedStorage is that the .Net framework is responsible for the security and the location. You as developer don’t have to check if you have rights or the location exists.

Using the IsolatedStorage

The following code snippet will create a filestream which you can use (just as you would use a normal file stream). The IsolatedStorage objects also provide some other methods which you can use to check if files exist or even create and use directories.

Creating a file

[code lang=”csharp”]
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream str = new IsolatedStorageFileStream("YourFile.xml", FileMode.Create);
//Your Write actions

str.Flush();
store.Close();
[/code]

Reading a file

[code lang=”csharp”]
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream str = new IsolatedStorageFileStream("YourFile.xml",FileMode.Open, FileAccess.Read);
//Your read actions

str.Close();

store.Close();

[/code]

Resources