How to Check is the CDROM is loaded

Will be posting article on how to create simple application, with full working code in C#
And welcome to anyone who want to post their own article
Post Reply
User avatar

Topic author
Superl
Site Admin
Site Admin
Posts: 1267
Joined: Sat Apr 16, 2011 7:49 am
Location: Montreal, Canada
Contact:

How to Check is the CDROM is loaded

#2277

Post by Superl » Sat Dec 19, 2015 6:02 am

Check is the CDROM is loaded

Introduction:
This is a method I use to check to see if any CDROM's on a system, are already loaded.

  1. //Namespaces needed
  2. using System;
  3. using System.Management;
  4.  
  5. /// <summary>
  6. /// method to check and see if a CD is in the drive
  7. /// </summary>
  8. /// <returns></returns>
  9. public List<String> IsCDROMLoaded()
  10. {
  11.      List<string> cdProperties = new List<string>();
  12.       //create a query to searech the system for a drive type of 5 (CDROM)
  13.      SelectQuery query = new SelectQuery("select * from win32_logicaldisk where drivetype=5");
  14.       //use the System.Management Namespace to execute the query using the
  15.       //ManagementObjectSearcher Object
  16.      ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(query);
  17.       //now loop through all items returned from the query
  18.      foreach (ManagementObject drives in moSearcher.Get())
  19.      {
  20.             //check for a volumename and serial number property
  21.           if ((drives["volumename"] != null) || (drives["volumeserialnumber"] != null))
  22.           {
  23.                cdProperties.Add(drives["volumename"].ToString());
  24.                cdProperties.Add(drives["volumeserialnumber"].ToString());
  25.           }
  26.           else
  27.           {
  28.                cdProperties.Add("There is no CD in the drive.");
  29.           }
  30.      }
  31.      return cdProperties;
  32. }


Come and say hello in here
Any donation will help click here please.

Have a nice day :103:

Post Reply

Return to “Coding Forum”

Who is online

Users browsing this forum: No registered users and 1 guest