c# – 检测光盘是否在DVD驱动器中

前端之家收集整理的这篇文章主要介绍了c# – 检测光盘是否在DVD驱动器中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有一种简单的方法可以检测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.

原文链接:https://www.f2er.com/csharp/97768.html

猜你在找的C#相关文章