CD/DVDROM Eject in VB.NET
Submitted by donbermoy on Tuesday, April 8, 2014 - 19:03.
In this tutorial, we will create a program that can eject the CD/DVD-ROM of a laptop or desktop computer that can also view the drive for this ROM.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Right Click ToolBox, click Choose Items, in the Com Components Tab, check "Windows Media Player" and then click Ok.
3. Next, add only one Button named Button1 and labeled it as "Eject". Insert a combobox named ComboBox1 for displaying the drive of the cdrom. You must design your interface like this:
4. Declare this global variable in your code module.
We just used the WindowsMediaPlayer library to open the cdrom collection which hold the varCDrom variable.
5. In your Form_Load, copy the code below. This will show all the drives for CDROM to be displayed in the combobox.
We initialized first the totalCDDrive variable and i variable to 0. Then the totalCDDrive variable will be equal to number of cdrom drives. Then the cdrom drive will be displayed in the combobox.
6. Put this code in your Button1_Click. This will trigger to eject the drive you have chosen in the combobox.
If the combobox is not empty, then the selected index cdrom drive in combobox is ejected as we have the property of cdromCollection has eject property.
- Dim varCDrom As New WMPLib.WindowsMediaPlayer
- Dim totalCDDrive As Integer
- Dim i As Integer
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- totalCDDrive = 0
- i = 0
- totalCDDrive = varCDrom.cdromCollection.count
- If (totalCDDrive >= 1) Then
- For i = 0 To (totalCDDrive - 1)
- ComboBox1.Items.Add(varCDrom.cdromCollection.Item(i).driveSpecifier)
- Next
- ComboBox1.Text = ComboBox1.Items.Item(0)
- End If
- End Sub
- If Not (ComboBox1.Text = "") Then
- varCDrom.cdromCollection.Item(ComboBox1.SelectedIndex).eject()
- End If
Output:
Download the source code below and try it! :) For more inquiries just contact my number or e-mail below. Best Regards,Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]
Visit and like my page on Facebook at: Bermz ISware Solutions
Subscribe at my YouTube Channel at: SerBermz
Add new comment
- 418 views