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.

444 ❘ CHAPTER 19 File System Objects<br />

Finally this chapter explained techniques you can use to manage the recycle bin. Some of them are<br />

fairly complex, but at least the FileIO.FileSystem class makes moving a file or directory into the<br />

recycle reasonably simple.<br />

So far the chapters in this book have explained how to do things locally on the user’s computer.<br />

The next chapter explains how a program can move off of the local computer to download files<br />

from the Internet.<br />

Exercises<br />

1. Write a program that lets the user select a directory and displays its creation, last access,<br />

and last write times.<br />

2. Write a program that lets the user select a file and displays its creation, last access, and last<br />

write times. Let the user change the times and then set them for the file.<br />

3. Write a program that lets the user get and set a file’s attributes. Use CheckBoxes to display<br />

and let the user specify attributes.<br />

4. Write a program that uses File.ReadAllText and File.WriteAllText to save and restore<br />

the contents of a text file when it starts and stops. Compare your solution to the solution for<br />

Exercise 18-3.<br />

5. Write a program that sorts the lines in a file. (Hint: Use File.ReadAllLines to get the<br />

lines, sort them, and then use File.WriteAllLines to write them back into the file.)<br />

6. Write a program that lists the computer’s drives and whatever drive information is available<br />

from the DriveInfo class.<br />

7. Modify the program you wrote for Exercise 6 so that it displays sizes in KB, MB, GB, or<br />

TB as appropriate.<br />

8. Write a program that displays the name of the directory two levels higher than the directory<br />

where the program is executing. What happens if you run the program near the top of the<br />

directory hierarchy, for example, in C:\<br />

9. Write a program that lets the user enter a directory path and a pattern. When the user clicks<br />

the Search button, the program should search the directory and its subdirectories for files<br />

matching the pattern.<br />

10. Modify the program you wrote for Exercise 9 so that it shows the selected filenames (without<br />

paths) and their sizes.<br />

11. Write a program that uses a FileSystemWatcher to watch the directory where the program<br />

is executing for changes. When a change occurs, the program should display the date and<br />

time, the type of change, and the changing file’s name.<br />

12. Write a program that lets the user enter a filename and then uses the FileIO class to move<br />

the file into the recycle bin. (That’s all many programs need to do anyway. You can use the<br />

recycle bin on your desktop to manage its contents.)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!