How to get CD/DVD Drive capabilities using WMI and C#

No.of Views1070
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  09 Sep 2010 07:09:16
Tag : CSharp , WMI
This article about how to Get Different capabilities of CD_Drive like read/write using WMI
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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. 

Image Loading

Code

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());
                }
            }
        }

 I hopes help.

Sample Project Source

Download source files -42 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:NosaDate Of Posted:9/15/2010 4:47:40 PM
Error
I tried to run your downloaded code. When I run it, the program crashed. I get the following error: --------------------------- --------------------------- Object reference not set to an instance of an object. --------------------------- OK --------------------------- How can I resolve this error? Nosa
By:NosaDate Of Posted:9/15/2010 4:46:39 PM
Error With Get CD Devices Capabilities
I tried to run your downloaded code. When I run it, the program crashed. I get the following error: --------------------------- --------------------------- Object reference not set to an instance of an object. --------------------------- OK --------------------------- How can I resolve this error? Nosa
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top