CD/DVDROM Eject using C#

This is a tutorial in which we will going to create a program that will eject the CDROM or DVDROM using C# only. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name the project CD eject. 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: design 4. Import AxWMPLib namespace as we access the Windows Media Player.
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8. using AxWMPLib;
5. Declare this global variable in your code module. We will just use the WindowsMediaPlayer library to open the cdrom collection which hold the varCDrom variable.
  1. WMPLib.WindowsMediaPlayer varCDrom = new WMPLib.WindowsMediaPlayer();
  2. int totalCDDrive;
  3. int i;
6. In your Form_Load, copy the code below. This will show all the drives for CDROM to be displayed in the combobox. We will initialize 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.
  1. public void Form1_Load(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         totalCDDrive = 0;
  4.                         i = 0;
  5.                         totalCDDrive = varCDrom.cdromCollection.count;
  6.                         if (totalCDDrive >= 1)
  7.                         {
  8.                                 for (i = 0; i <= (totalCDDrive - 1); i++)
  9.                                 {
  10.                                         ComboBox1.Items.Add(varCDrom.cdromCollection.Item(i).driveSpecifier);
  11.                                 }
  12.                                 ComboBox1.Text = (string) (ComboBox1.Items[0]);
  13.                         }
  14.                        
  15.                 }
7. 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.
  1. public void Button1_Click_1(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         if (!(ComboBox1.Text == ""))
  4.                         {
  5.                                 varCDrom.cdromCollection.Item(ComboBox1.SelectedIndex).eject();
  6.                         }
  7.                 }
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8. using AxWMPLib;
  9.  
  10.  
  11.  
  12. namespace CD_eject
  13. {
  14.         public partial class Form1
  15.         {
  16.                 public Form1()
  17.                 {
  18.                         InitializeComponent();
  19.                        
  20.                 }
  21.                
  22.                 WMPLib.WindowsMediaPlayer varCDrom = new WMPLib.WindowsMediaPlayer();
  23.                 int totalCDDrive;
  24.                 int i;
  25.                
  26.                 public void Form1_Load(System.Object sender, System.EventArgs e)
  27.                 {
  28.                         totalCDDrive = 0;
  29.                         i = 0;
  30.                         totalCDDrive = varCDrom.cdromCollection.count;
  31.                         if (totalCDDrive >= 1)
  32.                         {
  33.                                 for (i = 0; i <= (totalCDDrive - 1); i++)
  34.                                 {
  35.                                         ComboBox1.Items.Add(varCDrom.cdromCollection.Item(i).driveSpecifier);
  36.                                 }
  37.                                 ComboBox1.Text = (string) (ComboBox1.Items[0]);
  38.                         }
  39.                        
  40.                 }
  41.                
  42.                 public void Button1_Click_1(System.Object sender, System.EventArgs e)
  43.                 {
  44.                         if (!(ComboBox1.Text == ""))
  45.                         {
  46.                                 varCDrom.cdromCollection.Item(ComboBox1.SelectedIndex).eject();
  47.                         }
  48.                 }
  49.         }
  50.        
  51. }
Output: output Download the source code below and try it! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment