15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

812 ❘ ChaPTer 29 mAnipulAtinG files And the reGistry<br />

}<br />

writer.WriteStartElement("Red");<br />

writer.WriteValue(BackColor.R);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("Green");<br />

writer.WriteValue(BackColor.G);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("Blue");<br />

writer.WriteValue(BackColor.B);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("Width");<br />

writer.WriteValue(Width);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("Height");<br />

writer.WriteValue(Height);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("X");<br />

writer.WriteValue(DesktopLocation.X);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("Y");<br />

writer.WriteValue(DesktopLocation.Y);<br />

writer.WriteEndElement();<br />

writer.WriteStartElement("WindowState");<br />

writer.WriteValue(WindowState.ToString());<br />

writer.WriteEndElement();<br />

writer.WriteEndElement();<br />

writer.Flush();<br />

writer.Close();<br />

storStream.Close();<br />

storFile.Close();<br />

It is a bit more code than you might be used to when working with the registry, but that is mainly due to the<br />

code required to build the XML document placed in isolated storage. The first important thing happening<br />

with this code is presented here:<br />

IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain();<br />

IsolatedStorageFileStream storStream = new<br />

IsolatedStorageFileStream("SelfPlacingWindow.xml",<br />

FileMode.Create, FileAccess.Write);<br />

Here, an instance of an IsolatedStorageFile is created using a user, assembly, <strong>and</strong> domain type of<br />

access. A stream is created using the IsolatedStorageFileStream object, which creates the virtual<br />

SelfPlacingWindow.xml file.<br />

From there, an XmlTextWriter object is created to build the XML document <strong>and</strong> the XML contents are<br />

written to the IsolatedStorageFileStream object instance:<br />

System.Xml.XmlTextWriter writer = new<br />

System.Xml.XmlTextWriter(storStream, Encoding.UTF8);<br />

After the XmlTextWriter object is created, all the values are written to the XML document node by node.<br />

When everything is written to the XML document, everything is closed <strong>and</strong> is stored in the isolated storage.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!