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.

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

}<br />

string query = "Really move the file\n" + filePath + "\nto "<br />

+ textBoxNewPath.Text + "";<br />

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

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

{<br />

File.Move(filePath, textBoxNewPath.Text);<br />

DisplayFolderList(currentFolderPath);<br />

}<br />

}<br />

catch(Exception ex)<br />

{<br />

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

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

}<br />

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

{<br />

try<br />

{<br />

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

textBoxFileName.Text);<br />

string query = "Really copy the file\n" + filePath + "\nto "<br />

+ textBoxNewPath.Text + "";<br />

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

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

{<br />

File.Copy(filePath, textBoxNewPath.Text);<br />

DisplayFolderList(currentFolderPath);<br />

}<br />

}<br />

catch(Exception ex)<br />

{<br />

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

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

}<br />

}<br />

code download FilePropertiesAndMovement.sln<br />

You are not quite done. You also need to make sure that the new buttons <strong>and</strong> text box are enabled <strong>and</strong><br />

disabled at the appropriate times. To enable them when you are displaying the contents of a file, you add the<br />

following code to DisplayFileInfo():<br />

protected void DisplayFileInfo(string fileFullName)<br />

{<br />

FileInfo theFile = new FileInfo(fileFullName);<br />

if (!theFile.Exists)<br />

{<br />

throw new FileNotFoundException("File not found: " + fileFullName);<br />

}<br />

textBoxFileName.Text = theFile.Name;<br />

textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();<br />

textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();<br />

textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();<br />

textBoxFileSize.Text = theFile.Length.ToString() + " bytes";<br />

// enable move, copy, delete buttons<br />

textBoxNewPath.Text = theFile.FullName;<br />

textBoxNewPath.Enabled = true;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!