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.

29<br />

Manipulating files<br />

<strong>and</strong> the registry<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Exploring the directory structure<br />

Moving, copying, <strong>and</strong> deleting fi les <strong>and</strong> folders<br />

Reading <strong>and</strong> writing text in fi les<br />

Reading <strong>and</strong> writing keys in the registry<br />

Reading <strong>and</strong> writing to isolated storage<br />

This chapter examines how to perform tasks involving reading from <strong>and</strong> writing to fi les <strong>and</strong> the <strong>C#</strong><br />

system registry. Microsoft has provided very intuitive object models covering these areas, <strong>and</strong> in this<br />

chapter, you learn how to use .<strong>NET</strong> base classes to perform the listed tasks. In the case of fi le system<br />

operations, the relevant classes are almost all found in the System.IO namespace, whereas<br />

registry operations are dealt with by classes in the Microsoft.Win32 namespace.<br />

The .<strong>NET</strong> base classes also include a number of classes <strong>and</strong> interfaces in the System<br />

.Runtime.Serialization namespace concerned with serialization — that is, the<br />

process of converting data (for example, the contents of a document) into a stream of<br />

bytes for storage. This chapter does not focus on these classes; it focuses on the classes<br />

that give you direct access to fi les.<br />

Note that security is particularly important when modifying either fi les or registry entries. Security<br />

is covered entirely in Chapter 21, “ Security. ” In this chapter, however, we assume that you have<br />

suffi cient access rights to run all the examples that modify fi les or registry entries, which should be<br />

the case if you are running from an account with administrator privileges.<br />

managing The file sysTem<br />

The classes that are used to browse around the fi le system <strong>and</strong> perform operations such as moving,<br />

copying, <strong>and</strong> deleting fi les are shown in Figure 29 - 1.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!