This is a method I use to check to see if any CDROM's on a system, are already loaded.
Code: Select all
//Namespaces needed
using System;
using System.Management;
/// <summary>
/// method to check and see if a CD is in the drive
/// </summary>
/// <returns></returns>
public List<String> IsCDROMLoaded()
{
List<string> cdProperties = new List<string>();
//create a query to searech the system for a drive type of 5 (CDROM)
SelectQuery query = new SelectQuery("select * from win32_logicaldisk where drivetype=5");
//use the System.Management Namespace to execute the query using the
//ManagementObjectSearcher Object
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(query);
//now loop through all items returned from the query
foreach (ManagementObject drives in moSearcher.Get())
{
//check for a volumename and serial number property
if ((drives["volumename"] != null) || (drives["volumeserialnumber"] != null))
{
cdProperties.Add(drives["volumename"].ToString());
cdProperties.Add(drives["volumeserialnumber"].ToString());
}
else
{
cdProperties.Add("There is no CD in the drive.");
}
}
return cdProperties;
}