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.

Moving, Copying, <strong>and</strong> Deleting files ❘ 781<br />

enabled only when the example is actually displaying the properties of a file;<br />

at all other times, they are disabled. The existing controls are also squashed<br />

up a bit to stop the main form from getting too big. When the properties of<br />

a selected file are displayed, FilePropertiesAndMovement automatically<br />

places the full pathname of that file in the bottom text box for the user to<br />

edit. Users can then click any of the buttons to perform the appropriate<br />

operation. When they do, a message box is displayed that confirms the action<br />

taken by the user (see Figure 29-5).<br />

figure 29-5<br />

When the user clicks the Yes button, the action is initiated. There are some<br />

actions in the form that the user can take that will then cause the display to<br />

be incorrect. For instance, if the user moves or deletes a file, you obviously cannot continue to display the<br />

contents of that file in the same location. In addition, if you change the name of a file in the same folder,<br />

your display will also be out of date. In these cases, FilePropertiesAndMovement resets its controls to<br />

display only the folder where the file resides after the file operation.<br />

looking at the Code for fileProperties<strong>and</strong>movement<br />

To code this process, you need to add the relevant controls, as well as their event h<strong>and</strong>lers, to the code<br />

for the FileProperties sample. The new controls are given the names buttonDelete, buttonCopyTo,<br />

buttonMoveTo, <strong>and</strong> textBoxNewPath.<br />

First, look at the event h<strong>and</strong>ler that is called when the user clicks the Delete button:<br />

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

{<br />

try<br />

{<br />

string filePath = Path.Combine(currentFolderPath,<br />

textBoxFileName.Text);<br />

string query = "Really delete the file\n" + filePath + y";<br />

if (MessageBox.Show(query,<br />

"Delete File", MessageBoxButtons.YesNo) == DialogResult.Yes)<br />

{<br />

File.Delete(filePath);<br />

DisplayFolderList(currentFolderPath);<br />

}<br />

}<br />

catch(Exception ex)<br />

{<br />

MessageBox.Show("Unable to delete file. The following exception"<br />

+ " occurred:\n" + ex.Message, "Failed");<br />

}<br />

}<br />

code download FilePropertiesAndMovement.sln<br />

The code for this method is contained in a try block because of the obvious risk of an exception being<br />

thrown if, for example, you don’t have permission to delete the file, or the file is moved by another process<br />

after it has been displayed but before the user presses the Delete button. You construct the path of the file to<br />

be deleted from the CurrentParentPath field, which contains the path of the parent folder, <strong>and</strong> the text in<br />

the textBoxFileName text box, which contains the name of the file.<br />

The methods to move <strong>and</strong> copy the file are structured in a very similar manner:<br />

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

{<br />

try<br />

{<br />

string filePath = Path.Combine(currentFolderPath,<br />

textBoxFileName.Text);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!