15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

}<br />

this.BackColor =<br />

Color.FromArgb(redComponent, greenComponent, blueComponent);<br />

this.DesktopLocation = new Point(X, Y);<br />

listBoxMessages.Items.Add("Background color: " + BackColor.Name);<br />

listBoxMessages.Items.Add("Desktop location: " +<br />

DesktopLocation.ToString());<br />

listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString());<br />

listBoxMessages.Items.Add("Window State: " + WindowState.ToString());<br />

storStream.Close();<br />

storFile.Close();<br />

}<br />

}<br />

return true;<br />

Using the GetFileNames() method, the SelfPlacingWindow.xml document is pulled from the isolated<br />

storage <strong>and</strong> then placed into a stream <strong>and</strong> parsed using the XmlTextReader object:<br />

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

string[] userFiles = storFile.GetFileNames("SelfPlacingWindow.xml");<br />

foreach (string userFile in userFiles)<br />

{<br />

if(userFile == "SelfPlacingWindow.xml")<br />

{<br />

listBoxMessages.Items.Add("Successfully opened file " +<br />

userFile.ToString());<br />

StreamReader storStream =<br />

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

FileMode.Open, storFile));<br />

After the XML document is contained within the IsolatedStorageFileStream object, it is parsed using<br />

the XmlTextReader object:<br />

System.Xml.XmlTextReader reader = new<br />

System.Xml.XmlTextReader(storStream);<br />

After, it is pulled from the stream via the XmlTextReader. The element values are then pushed back into<br />

the application. You will then find — just as was accomplished in the SelfPlacingWindow sample that<br />

used the registry to record <strong>and</strong> retrieve application state values — that using isolated storage is just as<br />

effective as working with the registry. The application remembers the color, size, <strong>and</strong> position just as<br />

before.<br />

summary<br />

In this chapter, you examined how to use the .<strong>NET</strong> base classes to access the file system <strong>and</strong> registry from<br />

your <strong>C#</strong> code. You have seen that in both cases the base classes expose simple, but powerful, object models<br />

that make it very simple to perform almost any kind of action in these areas. For the file system, these<br />

actions are copying files; moving, creating, <strong>and</strong> deleting files <strong>and</strong> folders; <strong>and</strong> reading <strong>and</strong> writing both<br />

binary <strong>and</strong> text files. For the registry, these are creating, modifying, or reading keys.<br />

This chapter also reviewed isolated storage <strong>and</strong> how to use this from your applications to store them in the<br />

application state.<br />

This chapter assumed that you were running your code from an account that has sufficient access rights<br />

to do whatever the code needs to do. Obviously, the question of security is an important one, <strong>and</strong> it is<br />

discussed in Chapter 21.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!