是否有一种简单的方法可以检测DVD驱动器中是否插入了光盘?我不在乎什么样的光盘(CD,DVD或蓝光)?
答案 0 :(得分:14)
使用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。 Fixed
:驱动器是固定磁盘。 Network
:驱动器是网络驱动器。 NoRootDirectory
驱动器没有根目录。 Ram
:驱动器是RAM磁盘。 Removable
:驱动器是可移动存储设备,例如软盘驱动器或USB闪存驱动器。 Unknown
:驱动器的类型未知。 有关CD / DVD /蓝光的信息,请参阅IMAPI_MEDIA_PHYSICAL_TYPE enumeration:
您的代码可能是这样的:
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。
答案 1 :(得分:1)
How to Detect CD-ROM is loaded in the CD-ROM drive
从上面链接
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();
}
}