374
LESSON 32 Using the Clipboard
// Create the directory if necessary.
dirInfo.Create();
// Process the les.
foreach (string lename in Clipboard.GetFileDropList())
{
FileInfo leInfo = new FileInfo(lename);
// Add a version number to the end.
// Keep trying until we nd one that isn’t
// already there.
for (int i = 0; ; i++)
{
// Compose a le name in the backup directory
// with the le’s name and the version number.
string newFileName =
Path.Combine(dirInfo.FullName, leInfo.Name) +
“.” + i.ToString(“000”);
// See if this le exists.
if (!File.Exists(newFileName))
{
// Copy the le here.
leInfo.CopyTo(newFileName);
// Exit the for loop.
break;
}
}
}
}
MessageBox.Show(“Done”);
}
Please select Lesson 32 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build the application shown in Figure 32-2. When the user presses
Ctrl+V, display any text or image that the clipboard contains.
Hints: Set the form’s KeyPreview property to true. Then use
code similar to the following to tell when the user presses
Ctrl+V. Notice that the code sets
e.Handled = true to indi-
cate that the key press can be discarded after the event handler
is done with it.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// See if it’s Ctrl+V.
FIGURE 322
596906c32.indd 374 4/7/10 12:34:36 PM