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.

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

}<br />

{<br />

}<br />

MessageBox.Show(ex.Message);<br />

code download FileProperties.sln<br />

Finally, when the Up button is clicked, DisplayFolderList() must also be called, except that this time<br />

you need to obtain the path of the parent of the folder currently being displayed. This is done with the<br />

FileInfo.DirectoryName property, which returns the parent folder path:<br />

protected void OnUpButtonClick(object sender, EventArgs e)<br />

{<br />

try<br />

{<br />

string folderPath = new FileInfo(currentFolderPath).DirectoryName;<br />

DisplayFolderList(folderPath);<br />

}<br />

catch(Exception ex)<br />

{<br />

MessageBox.Show(ex.Message);<br />

}<br />

}<br />

code download FileProperties.sln<br />

moVing, CoPying, <strong>and</strong> deleTing files<br />

As mentioned, moving <strong>and</strong> deleting files or folders is done by the MoveTo() <strong>and</strong> Delete() methods of the<br />

FileInfo <strong>and</strong> DirectoryInfo classes. The equivalent methods on the File <strong>and</strong> Directory classes are<br />

Move() <strong>and</strong> Delete(). The FileInfo <strong>and</strong> File classes also implement the methods CopyTo() <strong>and</strong> Copy(),<br />

respectively. However, no methods exist to copy complete folders — you need to do that by copying each file<br />

in the folder.<br />

Using all these methods is quite intuitive —<br />

you can find detailed descriptions in the SDK<br />

documentation. This section illustrates their<br />

use for the particular cases of calling the static<br />

Move(), Copy(), <strong>and</strong> Delete() methods on<br />

the File class. To do this, you will build on the<br />

previous FileProperties example <strong>and</strong> call<br />

its iteration FilePropertiesAndMovement.<br />

This example will have the extra feature that<br />

whenever the properties of a file are displayed,<br />

the application gives you the option of deleting<br />

that file or moving or copying the file to another<br />

location.<br />

fileProperties<strong>and</strong>movement sample<br />

Figure 29-4 shows the user interface of the new<br />

sample application.<br />

As you can see, FilePropertiesAndMovement is<br />

similar in appearance to FileProperties, except<br />

for the group of three buttons <strong>and</strong> a text box at<br />

the bottom of the window. These controls are figure 29-4<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!