Reading and Writing to Isolated Storage


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 termed as 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.

You can think of isolated storage is 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 by 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. Figure 24-21 shows this in a diagram.

image from book
Figure 24-21

This means that you can have multiple instances of the same application all working from the same store. Another type of access for isolated storage is by user, assembly, and domain. In this case, each application instance will work off its own isolation store. This is detailed in Figure 24-22.

image from book
Figure 24-22

In this case, each application instance works off of its own store, and the settings 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.NET application just as well), let’s change the SelfPlacingWindow example that was previously used in this chapter to illustrate how to record information to the registry. Though a new ReadSettings() and SaveSettings() method, you read and write values to isolated storage as opposed to doing the same to the registry.

Tip 

It is important to note that the only code shown here is for the ReadSettings() and SaveSettings() methods. There is more code to the application and you can see the rest of the code in the previous exampletitled “Example: SelfPlacingWindow.”

To start, you will need to rework the SaveSettings() method. This method is detailed in the following code example:

  void SaveSettings() {    IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain();    IsolatedStorageFileStream storStream = new       IsolatedStorageFileStream("SelfPlacingWindow.xml",       FileMode.Create, FileAccess.Write);    System.Xml.XmlTextWriter writer = new       System.Xml.XmlTextWriter(storStream, Encoding.UTF8);    writer.Formatting = System.Xml.Formatting.Indented;    writer.WriteStartDocument();    writer.WriteStartElement("Settings");    writer.WriteStartElement("BackColor");    writer.WriteValue(BackColor.ToKnownColor().ToString());    writer.WriteEndElement();    writer.WriteStartElement("Red");    writer.WriteValue(BackColor.R);    writer.WriteEndElement();    writer.WriteStartElement("Green");    writer.WriteValue(BackColor.G);    writer.WriteEndElement();    writer.WriteStartElement("Blue");    writer.WriteValue(BackColor.B);    writer.WriteEndElement();    writer.WriteStartElement("Width");    writer.WriteValue(Width);    writer.WriteEndElement();    writer.WriteStartElement("Height");    writer.WriteValue(Height);    writer.WriteEndElement();    writer.WriteStartElement("X");    writer.WriteValue(DesktopLocation.X);    writer.WriteEndElement();    writer.WriteStartElement("Y");    writer.WriteValue(DesktopLocation.Y);    writer.WriteEndElement();    writer.WriteStartElement("WindowState");    writer.WriteValue(WindowState.ToString());    writer.WriteEndElement();    writer.WriteEndElement();    writer.Flush();    writer.Close();    storStream.Close();    storFile.Close(); } 

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 bit of code is presented here:

 IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); IsolatedStorageFileStream storStream = new    IsolatedStorageFileStream("SelfPlacingWindow.xml",    FileMode.Create, FileAccess.Write);

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 XmlTextWriter object is created to build the XML document and the XML contents are written to the IsolatedStorageFileStream object instance.

 System.Xml.XmlTextWriter writer = new    System.Xml.XmlTextWriter(storStream, Encoding.UTF8);

After the XmlTextWriter 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:

  bool ReadSettings() {    IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain();    string[] userFiles = storFile.GetFileNames("SelfPlacingWindow.xml");    foreach (string userFile in userFiles)    {       if(userFile == "SelfPlacingWindow.xml")       {          listBoxMessages.Items.Add("Successfully opened file " +                                     userFile.ToString());          StreamReader storStream =             new StreamReader(new IsolatedStorageFileStream("SelfPlacingWindow.xml",             FileMode.Open, storFile));          System.Xml.XmlTextReader reader = new             System.Xml.XmlTextReader(storStream);          int redComponent = 0;          int greenComponent = 0;          int blueComponent = 0;          int X = 0;          int Y = 0;          while (reader.Read())          {             switch (reader.Name)             {                 case "Red":                    redComponent = int.Parse(reader.ReadString());                    break;                 case "Green":                    greenComponent = int.Parse(reader.ReadString());                    break;                 case "Blue":                    blueComponent = int.Parse(reader.ReadString());                    break;                 case "X":                    X = int.Parse(reader.ReadString());                    break;                 case "Y":                    Y = int.Parse(reader.ReadString());                    break;                 case "Width":                    this.Width = int.Parse(reader.ReadString());                    break;                 case "Height":                    this.Height = int.Parse(reader.ReadString());                    break;                 case "WindowState":                    this.WindowState = (FormWindowState)FormWindowState.Parse                      (WindowState.GetType(), reader.ReadString());                    break;                 default:                    break;             }          }          this.BackColor =             Color.FromArgb(redComponent, greenComponent, blueComponent);          this.DesktopLocation = new Point(X, Y);          listBoxMessages.Items.Add("Background color: " + BackColor.Name);          listBoxMessages.Items.Add("Desktop location: " +             DesktopLocation.ToString());          listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString());          listBoxMessages.Items.Add("Window State: " + WindowState.ToString());          storStream.Close();          storFile.Close();          return true;    } } 

Using the GetFileNames() method, the SelfPlacingWindow.xml document is pulled from the isolated storage and then placed into a stream and parsed using the XmlTextReader object.

 IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); string[] userFiles = storFile.GetFileNames("SelfPlacingWindow.xml"); foreach (string userFile in userFiles) {    if(userFile == "SelfPlacingWindow.xml")    {       listBoxMessages.Items.Add("Successfully opened file " +                                  userFile.ToString());       StreamReader storStream =          new StreamReader(new IsolatedStorageFileStream("SelfPlacingWindow.xml",          FileMode.Open, storFile));

Once the XML document is contained within the IsolatedStorageFileStream object, it is parsed using the XmlTextReader object:

 System.Xml.XmlTextReader reader = new    System.Xml.XmlTextReader(storStream);

After, it is pulled from the stream via the XmlTextReader. 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. The application will remember the color, size, and position just as before.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net