In Focus

Getting CD/DVD Drive capabilities using WMI and C#

article about how to Get Different capabilities of CD_Drive like read/write using WMI


Get CD/DVD Drive capabilities using WMI

Here  I am presenting Article on how to get CD drive capabilities like whether  it is providing Writing / Reading etc capabilities   or not like you have seen the Nero Burning  providing such information same from the WMI.

To get such information we need to Query management classes through sql like query for getting such information.

To work with WMI in windows form first of all we need to reference System.Management Library so add reference to it by

Project Menu>>Add reference in visual Studio project.

now we need to know in which class the CD Drive information is there so  information about CD Drive is provided by

Win32_CDROMDrive

 

Class of WMI so we need to query that class to get desired information.

I have setup a GUI Interface like taken One List Box one combo box to Show Drive that user can select and one button like below


private void Form1_Load(object sender, EventArgs e)

{

 

   ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");

   foreach (ManagementObject mo in mos.Get())

   {

       comboBox1.Items.Add(mo["Caption"].ToString());

   }

}

 

private void btnGetCapability_Click(object sender, EventArgs e)

{

       ManagementObjectSearcher mos;

       mos = new ManagementObjectSearcher("Select * from Win32_CDROMDrive where Caption='" + comboBox1.SelectedItem.ToString() + "'");

 

      foreach (ManagementObject mo in mos.Get())

      {

           string[] x = (string[])mo["CapabilityDescriptions"];

           foreach(string s in x)

           {

              listBox1.Items.Add(s);

           }

      }

}

Code Explanation:

Code is pretty simple we first make query about all the CD/Drives and filled the name of all the drives in combo box so user can select it now based on that name we again done another query with specific name with where clause and got specific management object now after getting management object we just need to get its various properties and we are done.

Here we are getting capabilities that returns array of string so its necessary to convert the Management Object to string array so we done it using casting it to String[] .

Another way you can do it with capabilities property but in this case we need to build a dictionary<> so that we can produce string value based on Uint1 Number returned by Management Object

private void Form1_Load(object sender, EventArgs e)

{

 

   ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");

   foreach (ManagementObject mo in mos.Get())

   {

       comboBox1.Items.Add(mo["Caption"].ToString());

   }

}


private void btnGetCapability_Click(object sender, EventArgs e)

        {

            Dictionary<UInt16, string> list = new Dictionary<UInt16, string>();

            list.Add(0, "Unknown");

            list.Add(1, "Other");

            list.Add(2, "Sequential Access");

            list.Add(3, "Random Access");

            list.Add(4, "Supports Writing");

            list.Add(5, "Encryption");

            list.Add(6, "Compression");

            list.Add(7, "Supports Removable Media");

            list.Add(8, "Manual Cleaning");

            list.Add(9, "Automatic Cleaning");

            list.Add(10, "SMART Notification");

            list.Add(11, "Supports Dual-Sided Media");

            list.Add(12, "Ejection Prior to Drive Dismount Not Required");

           

           

            ManagementObjectSearcher mos;

            mos = new ManagementObjectSearcher("Select * from Win32_CDROMDrive where Caption='" + comboBox1.SelectedItem.ToString() + "'");

 

            foreach (ManagementObject mo in mos.Get())

            {

                UInt16[] x = (UInt16[])mo["Capabilities"];

                foreach(UInt16 i in x)

                {

                     listBox1.Items.Add(list[i].ToString());

                }

            }

        }

thanks :)