是否有一种简单的方法可以检测DVD驱动器中是否插入了光盘?我不在乎什么样的光盘(CD,DVD或蓝光)?
解决方法
使用WMI检测CD / DVD驱动器中的磁盘:
foreach (var drive in DriveInfo.GetDrives() .Where(d => d.DriveType == DriveType.CDRom)) MessageBox.Show(drive.Name + " " + drive.IsReady.ToString());
从here.
DriveType Enumeration可以帮你什么样的光盘:
> CDRom:驱动器是光盘设备,例如CD或DVD-ROM.
>修正:驱动器是固定磁盘.
>网络:驱动器是网络驱动器.
> NoRootDirectory驱动器没有根目录.
> Ram:驱动器是RAM磁盘.
>可移动:驱动器是可移动存储设备,例如软盘驱动器或USB闪存驱动器.
>未知:驱动器类型未知.
对于CD / DVD /蓝光的种类见IMAPI_MEDIA_PHYSICAL_TYPE enumeration:
>未知
> CDROM
> CDR
> CDRW
> DVDROM
> DVDRAM
> DVDPLUSR
> DVDPLUSRW
> DVDPLUSR_DUALLAYER
> DVDDASHR
> DVDDASHRW
> DVDDASHR_DUALLAYER
>磁盘
> DVDPLUSRW_DUALLAYER
> HDDVDROM
> HDDVDR
> HDDVDRAM
> BDROM
> BDR
> BDRE
> MAX
你的代码可能是这样的:
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; }
从here.