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
Man of action
Man of action
Posts: 1331
Joined: Sat Apr 16, 2011 7:49 am
12
Location: Montreal, Canada
Contact:

How to Check is the CDROM is loaded

#2277

Post by Superl »

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.

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


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

Have a nice day :103:
Post Reply

Return to “Coding Forum”