03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 ❘ 693<br />

8. The ShowGrandparent example program uses the following code to do this.<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

grandparentTextBox.Text = Path.GetFullPath(<br />

Path.Combine(Application.StartupPath, "..\\.."));<br />

}<br />

If you run the program near the top of the directory hierarchy, the result is simply the top<br />

of the hierarchy. For example, if you run the program in the directory C:\, the program<br />

displays C:\.<br />

9. The FindFiles example program uses the following code to do this.<br />

private void searchButton_Click(object sender, EventArgs e)<br />

{<br />

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

foreach (string filename in Directory.GetFiles(<br />

directoryTextBox.Text, patternTextBox.Text,<br />

SearchOption.AllDirectories))<br />

{<br />

filesListBox.Items.Add(filename);<br />

}<br />

}<br />

10. The FindFilesAndSizes example program uses the following code to do this.<br />

private void searchButton_Click(object sender, EventArgs e)<br />

{<br />

string results = "";<br />

foreach (string filename in Directory.GetFiles(<br />

directoryTextBox.Text, patternTextBox.Text,<br />

SearchOption.AllDirectories))<br />

{<br />

FileInfo info = new FileInfo(filename);<br />

}<br />

results += info.Name + "\r\n";<br />

results += " " + ToFileSize(info.Length) + "\r\n";<br />

}<br />

filesTextBox.Text = results;<br />

11. The WatchForChanges example program uses the following event handlers to display<br />

information about directory changes in the resultsTextBox.<br />

private void myFileSystemWatcher_Changed(object sender, FileSystemEventArgs e)<br />

{<br />

resultsTextBox.AppendText(<br />

DateTime.Now.ToShortDateString() + ": Changed " +<br />

e.Name + "\r\n");<br />

}<br />

private void myFileSystemWatcher_Created(object sender, FileSystemEventArgs e)<br />

{<br />

resultsTextBox.AppendText(<br />

DateTime.Now.ToShortDateString() + ": Created " +<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!