Tallan Blog

Tallan’s Experts Share Their Knowledge on Technology, Trends and Solutions to Business Challenges

Working with Isolated Storage in .NET

What is Isolated Storage and Why use It?

Putting it simply, an isolated storage is a “place” where an application can store its state information- things such as user settings, some assembly and application configuration information, and other application/assembly state information. So what is so different about that right?

Well a few things and some of these pertain to or relate to application and computer security. Let me explain, usually when an application tries to persist application state, it has to deal with user access rights, trust levels and so on. One way to resolve this is to give the application whatever rights it needs and potentially open up your computer to malware and spyware. Another way is to save information in the database. Now, the latter might be a viable option but in some cases complicates matters. Remeber when you just wanted to save some user settings to the registry (HKEY_LOCAL_USER) or an INI file. Writing to a registry and file system permissions and access is something that we have all gone through. Isolated storage provides some solutions to these problems.

The bottom line here is how would you like it if your application could persist its state without worrying about trust levels, disk permissions etc. and at the same time do it in a limited security sandbox (i.e. without risking the security of the computer)? This is where you say “What is Isolated Storage for 200 please?” and then your boss goes that is our “Daily Double” we are going to bump up your salary by 10% and make you the director of software development. Ok maybe not so drammatic but hey atleast he/she will be happy that you have a potential winner here!

When to use it?

Some of the caveats to Isolated Storage are related to the intended use of isolated storage. They are useful but I quote Peter Aitken from Dev Source “It is not a panacea”! As I mentioned before there are some good uses of Isolated Storage:

  • When you are trying to store user specific information such as user settings, relatively small size application level/assembly specific data.
  • You want to store state information without giving the application permissive user rights
  • You can live within User Quota restrictions set for isolated storage (Isolated storage is not suited for large data sets).
  • You dont want to or cannot (permissions) write information under your Application Directory (..Program Files\..)
  • You want to store data at different scopes – Application, Assembly, User

So What does .NET Offer?

.Net offers many mechanisms for running with lower privileged user access levels, however we are specifically talking about isolated storage here.

The System.IO namespace provides two main classes to manage Isolated Storage:

  • System.IO.IsolatedStorage.IsolatedStorageFile
  • System.IO.IsolatedStorage.IsolatedStorageFileStream
  • System.IO.IsolatedStorage.IsolatedStorageFilePermission

Although the former is named IsolatedStorageFile, it is really a store where you can create files and directories (Similar to System.IO.Directory and System.IO.File). The latter is a FileStream based class that is used to write files to and read files from the store. Since it is derived from System.IO.FileStream you can use all the FileStream methods that you are already familiar with. To Read and Write to the stream you will use the StreamReader and StreamWriter respectively (as usual) or variations there of (Such as XmlReader XmlWriter for Xml files).

Additional Information

Before diving into the how to create stores and files, I would like to summarize a few key points that you need to bear in mind:

  1. Scope: When considering isolated storage, you need to determine how you want to scope the data to be stored.
    • Application Level Data: Machine/Assembly scope. The store in this case you would store information that is specific to the assembly and local machine.
    • User Level Data: Assembly/User scope. The store in this case would store information that is specific to the assembly and user. Note: If you need to specify the user of the store, you will have to use impersonation, otherwise the store will be created for the current user of the system. If someone else logs in, then another store will be created for that user.
  2. Click-Once Applications
    • Click once applications are deployed via web pages. For click-once applications, an application level store is also supported. Application stores only work for click-once applications.
  3. File Permissions
    • The IsolatedStorageFilePermission class encapsulates the permission that can be granted to code for isolated store access. It has properties that allow you to specify the size of store per user and type of usage.
  4. Class Attributes/Annotations
    • The IsolatedStorageFilePermission class also can be used as an annotation for classes that intend to use isolated storage. The purpose of annotating you class is so as to ensure that any isolated store access from within this class will succeed. It also helps debug permission related issues if access is denied.

Creating a file in Isolated Storage

Step 1. Instantitate/Create an isolated storage store. The following code snippet creates a store at the user scope.

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();

Step 2. Create a isolated storage file stream.

Vista: C:\Users\<username>\AppData\Local\IsolatedStorage
XP: C:\Documents and Settings\<username>\Local Settings\Application Data\IsolatedStorage

IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("UserSettings.set", FileMode.Create, store);

Step 3. Write something to the file

//Write some user data to the stream
 StreamWriter sw = new StreamWriter(fileStream);
 sw.Write("User Preferences");

Step 4. Close the Stream

//close the stream writer

Step 5. Test if the file was written – Reading the file

string[] fileNames = store.GetFileNames("*");if (fileNames.Length == 0)
 throw new FileNotFoundException("The Isolated storage file was not created");
foreach (string s in fileNames)
 Console.WriteLine("Contents of: {0}", s);
//Read the file
 IsolatedStorageFileStream stream = new IsolatedStorageFileStream(s, FileMode.Open, store);
//print the contents
 StreamReader sr = new StreamReader(stream);
//close the store

Learn more about Tallan or see us in person at one of our many Events!


Share this post:

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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>