Is there an easy way to detect if a disc is inserted in the DVD drive? I don't care what kind of disc (CD, DVD or Blu-Ray)?
Use WMI to detect if disk in CD/DVD drive:
foreach (var drive in DriveInfo.GetDrives()
.Where(d => d.DriveType == DriveType.CDRom))
MessageBox.Show(drive.Name + " " + drive.IsReady.ToString());
from here.
DriveType Enumeration can help you what kind of disc:
CDRom
: The drive is an optical disc device, such as a CD or DVD-ROM. Fixed
: The drive is a fixed disk. Network
: The drive is a network drive. NoRootDirectory
The drive does not have a root directory. Ram
: The drive is a RAM disk. Removable
: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive. Unknown
: The type of drive is unknown. for kind of CD/DVD/Blue-Ray see IMAPI_MEDIA_PHYSICAL_TYPE enumeration:
your code may be like this:
public bool IsDiscAvailable(int driveNumber)
{
MsftDiscMaster2Class discMaster = new MsftDiscMaster2Class();
string id = discMaster[driveNumber];
MsftDiscRecorder2Class recorder = new MsftDiscRecorder2Class();
recorder.InitializeDiscRecorder(id);
MsftDiscFormat2DataClass dataWriter = new MsftDiscFormat2DataClass();
if (dataWriter.IsRecorderSupported(recorder))
{
dataWriter.Recorder = recorder;
}
else
{
Console.WriteLine("Recorder not supported");
return false;
}
if (dataWriter.IsCurrentMediaSupported(recorder))
{
var media = dataWriter.CurrentPhysicalMediaType;
if (media == IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_UNKNOWN)
{
Console.WriteLine("Unknown media or no disc");
}
else
{
Console.WriteLine("Found disc type {0}", media);
return true;
}
}
else
{
Console.WriteLine("Disc absent or invalid.");
}
return false;
}
from here.
How to Detect CD-ROM is loaded in the CD-ROM drive
From above link
using System;
using System.Management;
class Application
{
public static void Main()
{
SelectQuery query =
new SelectQuery( "select * from win32_logicaldisk where drivetype=5" );
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
foreach( ManagementObject mo in searcher.Get() )
{
// If both properties are null I suppose there's no CD
if(( mo["volumename"] != null) || (mo["volumeserialnumber"] != null))
{
Console.WriteLine("CD is named: {0}", mo["volumename"]);
Console.WriteLine("CD Serial Number: {0}", mo["volumeserialnumber"]);
}
else
{
Console.WriteLine("No CD in Unit");
}
}
// Here to stop app from closing
Console.WriteLine("\nPress Return to exit.");
Console.Read();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With