230 Part II Programming Fundamentals
Tip You’ll find the Fileopen .bmp file, along with the Disc Drive Error project, in the
C:\Vb10sbs\Chap09 folder.
To complete the exercise, you’ll need to be able to remove the CD or DVD, or connect
and disconnect your external storage device, as test conditions dictate, and you’ll need to
modify the program code with the drive letter you’re using. You’ll use the CD or DVD (or
equivalent media) throughout the chapter to force run-time errors and recover from them.
Experiment with disc drive errors
- Insert a blank CD or DVD in drive D (or the drive in which you create CDs or DVDs),
and copy the Fileopen .bmp file to it.
Use Windows Explorer or a third-party CD or DVD creation program to copy the file
and burn the disc. If you’re using a different external storage device, connect the device
or insert a blank disc, copy Fileopen .bmp to it, and make a note of the drive letter that
Windows assigns to the device. - Start Visual Studio, and then open the Disc Drive Error project, which is located in the
C:\Vb10sbs\Chap09\Disc Drive Error folder.
The Disc Drive Error project opens in the IDE. - If the project’s form isn’t visible, display it now.
The Disc Drive Error project is a skeleton program that displays the Fileopen .bmp file
in a picture box when the user clicks the Check Drive button. I designed the project as
a convenient way to create and trap run-time errors, and you can use it throughout
this chapter to build error handlers by using the Try... Catch code block.
- Double-click the Check Drive button on the form to display the Button1_Click event
procedure.
You’ll see the following line of program code between the Private Sub and End Sub
statements:
PictureBox1.Image = _
System.Drawing.Bitmap.FromFile("d:\fileopen.bmp")
As you’ve learned in earlier chapters, the FromFile method opens the specified file.
This particular use of FromFile opens the Fileopen .bmp file on drive D and displays it
in a picture box. However, if the CD or DVD is missing, the CD or DVD tray is open, the
file is not on the CD or DVD, or there is another problem with the path or drive letter
specified in the code, the statement produces a “File Not Found” error in Visual Basic.
This is the run-time error we want to trap.
- If your CD or DVD drive or attached peripheral device is using a drive letter other than “D”
now, change the drive letter in this program statement to match the letter you’re using.