In addition to being able to read and write to and from the registry, another option is reading and writing values to and from what is called isolated storage. If you are having issues writing to the registry or to disk in general, then isolated storage is where you should turn, You can use isolated storage to store application state or user settings quite easily.
Think of isolated storage as a virtual disk where you can save items that can be shared only by the application that created them, or with other application instances, There are two types of access types for isolated storage, The first is user and assembly.
When accessing isolated storage by user and assembly, there is a single storage location on the machine, which is accessible via multiple application instances, Access is guaranteed through the user identity and the application (or assembly) identity.
This means that you can have multiple instances of the same application all working from the same store.
The second type of access for isolated storage is user, assembly, and domain. In this case, each application instance will work off its own isolation store,This is detailed in Figure 25-22.
In this case, each application instance works off its own store, and the settings that each application instance records are related only to itself, This is a more fine-grained approach to isolated storage.
For an example of using isolated storage from a Windows Forms application (although you can use this from an’ASP.NETapplication just as well), change the SelfPlacingWindow example that was previously used in this chapter to illustrate how to record information to the registry, Through a new ReadSettings () and SaveSettings () method, you read and write values to isolated storage as opposed to doing the same to the registry.
It is important to note that the only code shown here is for the ReadSettings () and savesetings ( )’ methods, There is more code to the application, and you can see the rest of the code in the previous example titled “Example: Self lacing Window. “
To start, you need to rework the SllveSettings () method, For this next bit of code to work, you need to add the following using directives:
using System. Text;
The SaveSettings () method is detailed in the following code example:
It is a bit more code than working with the registry example, but that is mainly due to the code required to build the XML document placed in isolated storage, The first important thing happening with this code is presented here:
Here, an instance of an IsolatedStorageFile is created using a user, assembly, and domain type of access, A stream is created using the IsolatedStorageFileStream object, which will create the virtual SelfPlacingWindow .xml file.
From there, an Xml TextWri ter object is created to build the XML document and the XML contents are written to the IsolatedStorageFileStream object instance:
System.Xml.XmlTextWriter writer = new
After the Xml TextWriter object is created, all the values are written to the XML document node by node, Once everything is written to the XML document, everything is closed and will now be stored in the isolated storage.
Reading from the storage is done through the ReadSettings () method, This method is presented in the following code sample:
Using the GetFileNames () method, the SelfPlacingWindow .Xml document is pulled from the isolated storage and then placed into a stream and parsed using the Xml TextReader object:
Once the XMLdocument is contained within the IsolatedStorageFileStream object, it is parsed using the Xml TextReader object:
System.Xml.XmlTextReader reader = new
After, it is pulled from the stream via the Xml TextReader. The element values are then pushed back into the application, You will now find – just as was accomplished in the SelfPlacingWindow example that used the registry to record and retrieve application state values – using isolated storage is just as effective as working with the registry, The application will remember the color, size, and position just as before.