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.

Managing the file system ❘ 777<br />

You create the project as a st<strong>and</strong>ard <strong>C#</strong> Windows application in Visual Studio 2010. Add the various<br />

text boxes <strong>and</strong> the list box from the Windows Forms area of the toolbox. You also renamed the<br />

controls with the more intuitive names of textBoxInput, textBoxFolder, buttonDisplay, buttonUp,<br />

listBoxFiles, listBoxFolders, textBoxFileName, textBoxCreationTime, textBoxLastAccessTime,<br />

textBoxLastWriteTime, <strong>and</strong> textBoxFileSize.<br />

Next, you need to indicate that you will be using the System.IO namespace:<br />

using System;<br />

using System.IO;<br />

using System.Windows.Forms;<br />

You need to do this for all the file-system-related examples in this chapter, but this part of the code will not<br />

be explicitly shown in the remaining examples. You then add a member field to the main form:<br />

public partial class Form1: Form<br />

{<br />

private string currentFolderPath;<br />

code download FileProperties.sln<br />

currentFolderPath stores the path of the folder whose contents are displayed in the list boxes.<br />

Next, you need to add event h<strong>and</strong>lers for the user-generated events. The possible user inputs are:<br />

➤<br />

➤<br />

➤<br />

➤<br />

User clicks the Display button — You need to determine whether what the user has typed in the main<br />

text box is the path to a file or folder. If it is a folder, you list the files <strong>and</strong> subfolders of this folder in<br />

the list boxes. If it is a file, you still do this for the folder containing that file, but you also display the<br />

file properties in the lower text boxes.<br />

User clicks a filename in the Files list box — You display the properties of this file in the lower<br />

text boxes.<br />

User clicks a folder name in the Folders list box — You clear all the controls <strong>and</strong> then display the<br />

contents of this subfolder in the list boxes.<br />

User clicks the Up button — You clear all the controls <strong>and</strong> then display the contents of the parent of<br />

the currently selected folder.<br />

Before you see the code for the event h<strong>and</strong>lers, here is the code for the methods that do all the work. First,<br />

you need to clear the contents of all the controls. This method is fairly self-explanatory:<br />

protected void ClearAllFields()<br />

{<br />

listBoxFolders.Items.Clear();<br />

listBoxFiles.Items.Clear();<br />

textBoxFolder.Text = "";<br />

textBoxFileName.Text = "";<br />

textBoxCreationTime.Text = "";<br />

textBoxLastAccessTime.Text = "";<br />

textBoxLastWriteTime.Text = "";<br />

textBoxFileSize.Text = "";<br />

}<br />

code download FileProperties.sln<br />

Next, you define a method, DisplayFileInfo(), that h<strong>and</strong>les the process of displaying the information for<br />

a given file in the text boxes. This method takes one parameter, the full pathname of the file as a String,<br />

<strong>and</strong> works by creating a FileInfo object based on this path:<br />

protected void DisplayFileInfo(string fileFullName)<br />

{<br />

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

if (!theFile.Exists)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!