5

如标题所述,我正在尝试确定驱动器是 DVD 驱动器还是 CD 驱动器。同样,我不是在询问媒体,而是在询问实际的驱动器本身...... DriveInfo 类指向的对象。

我认为 DriveInfo 类还不够,我将不得不从 WMI 获取 Win32_DiskDrive 实例并在那里挖掘。问题是到目前为止我发现的所有样本都在谈论检测到的媒体,但是 DVD 驱动器可以使用 CD 媒体,所以这还不够。另外,无论媒体是否存在,代码都应该工作。

4

1 回答 1

1

您可以从 WMI 获得它。Win32_CDROMDrive 类。

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT MediaType FROM Win32_CDROMDrive");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementBaseObject obj in collection)
{
    MessageBox.Show(obj["MediaType"].ToString());
}

这将是一个问题,如果您需要了解 BDRom 等。我建议使用 NeroCmd 工具而不是 WMI。

你也可以试试看这里。这是关于刻录的,但您可以在那里找到支持的驱动器媒体。如果它支持蓝光媒体,那么它显然不是 CD-ROM :)

我试图只得到重要的部分。在这里,我们使用 IMAPI2 API 来获取设备功能(CD/DVD/BD/HDDVD-READ)。我知道,有支持 BD 和 HDDVD 的 COMBO 驱动器,但我没有在代码中显示它。

//
// Determine the current recording devices
//
MsftDiscMaster2 discMaster = null;
try
{
    discMaster = new MsftDiscMaster2();

    if (!discMaster.IsSupportedEnvironment)
        return;

    // Get drives
    foreach (string uniqueRecorderId in discMaster)
    {
        IDiscRecorder2 discRecorder2 = new MsftDiscRecorder2();
        discRecorder2.InitializeDiscRecorder(uniqueRecorderId);
        // Show device mount drive and determine type
        Console.WriteLine("Path: {0} Type: {1}", discRecorder2.VolumePathNames[0], CheckType(discRecorder2));
    }
}
catch (COMException ex)
{
    Console.WriteLine("Error:{0} - Please install IMAPI2", ex.ErrorCode);
}
finally
{
    if (discMaster != null)
        Marshal.ReleaseComObject(discMaster);
}

魔法^_^

// Determine type
static string CheckType(IDiscRecorder2 disc)
{
    var features = disc.SupportedFeaturePages.Select(a => (IMAPI_FEATURE_PAGE_TYPE)a).ToArray();
    if (features.Contains(IMAPI_FEATURE_PAGE_TYPE.IMAPI_FEATURE_PAGE_TYPE_HD_DVD_READ))
        return "HD-DVD-ROM";
    if (features.Contains(IMAPI_FEATURE_PAGE_TYPE.IMAPI_FEATURE_PAGE_TYPE_BD_READ))
        return "BD-ROM";
    if (features.Contains(IMAPI_FEATURE_PAGE_TYPE.IMAPI_FEATURE_PAGE_TYPE_DVD_READ))
        return "DVD-ROM";
    if (features.Contains(IMAPI_FEATURE_PAGE_TYPE.IMAPI_FEATURE_PAGE_TYPE_CD_READ))
        return "CD-ROM";
    return "Unknown";
}

互操作...

/// <summary>
/// Microsoft IMAPIv2 Disc Master
/// </summary>
[ComImport]
[Guid("27354130-7F64-5B0F-8F00-5D77AFBE261E")]
[CoClass(typeof(MsftDiscMaster2Class))]
public interface MsftDiscMaster2 : IDiscMaster2 //, DiscMaster2_Event
{
}

[ComImport, ComSourceInterfaces("DDiscMaster2Events\0")]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
[Guid("2735412E-7F64-5B0F-8F00-5D77AFBE261E")]
[ClassInterface(ClassInterfaceType.None)]
public class MsftDiscMaster2Class
{
}

[ComImport]
[CoClass(typeof(MsftDiscRecorder2Class))]
[Guid("27354133-7F64-5B0F-8F00-5D77AFBE261E")]
public interface MsftDiscRecorder2 : IDiscRecorder2
{
}


[ComImport]
[Guid("2735412D-7F64-5B0F-8F00-5D77AFBE261E")]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
[ClassInterface(ClassInterfaceType.None)]
public class MsftDiscRecorder2Class
{
}

/// <summary>
/// IDiscMaster2 is used to get an enumerator for the set of CD/DVD (optical) devices on the system
/// </summary>
[ComImport]
[Guid("27354130-7F64-5B0F-8F00-5D77AFBE261E")]
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FNonExtensible)]
public interface IDiscMaster2
{
    // Enumerates the list of CD/DVD devices on the system (VT_BSTR)
    [DispId(-4), TypeLibFunc((short)0x41)]
    IEnumerator GetEnumerator();

    // Gets a single recorder's ID (ZERO BASED INDEX)
    [DispId(0)]
    string this[int index] { get; }

    // The current number of recorders in the system.
    [DispId(1)]
    int Count { get; }

    // Whether IMAPI is running in an environment with optical devices and permission to access them.
    [DispId(2)]
    bool IsSupportedEnvironment { get; }
}

/// <summary>
///  Represents a single CD/DVD type device, and enables many common operations via a simplified API.
/// </summary>
[ComImport]
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FNonExtensible)]
[Guid("27354133-7F64-5B0F-8F00-5D77AFBE261E")]
public interface IDiscRecorder2
{
    // Ejects the media (if any) and opens the tray
    [DispId(0x100)]
    void EjectMedia();

    // Close the media tray and load any media in the tray.
    [DispId(0x101)]
    void CloseTray();

    // Acquires exclusive access to device.  May be called multiple times.
    [DispId(0x102)]
    void AcquireExclusiveAccess(bool force, string clientName);

    // Releases exclusive access to device.  Call once per AcquireExclusiveAccess().
    [DispId(0x103)]
    void ReleaseExclusiveAccess();

    // Disables Media Change Notification (MCN).
    [DispId(260)]
    void DisableMcn();

    // Re-enables Media Change Notification after a call to DisableMcn()
    [DispId(0x105)]
    void EnableMcn();

    // Initialize the recorder, opening a handle to the specified recorder.
    [DispId(0x106)]
    void InitializeDiscRecorder(string recorderUniqueId);

    // The unique ID used to initialize the recorder.
    [DispId(0)]
    string ActiveDiscRecorder { get; }

    // The vendor ID in the device's INQUIRY data.
    [DispId(0x201)]
    string VendorId { get; }

    // The Product ID in the device's INQUIRY data.
    [DispId(0x202)]
    string ProductId { get; }

    // The Product Revision in the device's INQUIRY data.
    [DispId(0x203)]
    string ProductRevision { get; }

    // Get the unique volume name (this is not a drive letter).
    [DispId(0x204)]
    string VolumeName { get; }

    // Drive letters and NTFS mount points to access the recorder.
    [DispId(0x205)]
    object[] VolumePathNames {[DispId(0x205)] get; }

    // One of the volume names associated with the recorder.
    [DispId(0x206)]
    bool DeviceCanLoadMedia {[DispId(0x206)] get; }

    // Gets the legacy 'device number' associated with the recorder.  This number is not guaranteed to be static.
    [DispId(0x207)]
    int LegacyDeviceNumber {[DispId(0x207)] get; }

    // Gets a list of all feature pages supported by the device
    [DispId(520)]
    object[] SupportedFeaturePages {[DispId(520)] get; }

    // Gets a list of all feature pages with 'current' bit set to true
    [DispId(0x209)]
    object[] CurrentFeaturePages {[DispId(0x209)] get; }

    // Gets a list of all profiles supported by the device
    [DispId(0x20a)]
    object[] SupportedProfiles {[DispId(0x20a)] get; }

    // Gets a list of all profiles with 'currentP' bit set to true
    [DispId(0x20b)]
    object[] CurrentProfiles {[DispId(0x20b)] get; }

    // Gets a list of all MODE PAGES supported by the device
    [DispId(0x20c)]
    object[] SupportedModePages {[DispId(0x20c)] get; }

    // Queries the device to determine who, if anyone, has acquired exclusive access
    [DispId(0x20d)]
    string ExclusiveAccessOwner {[DispId(0x20d)] get; }
}

public enum IMAPI_FEATURE_PAGE_TYPE
{
    IMAPI_FEATURE_PAGE_TYPE_PROFILE_LIST = 0,
    IMAPI_FEATURE_PAGE_TYPE_CORE = 1,
    IMAPI_FEATURE_PAGE_TYPE_MORPHING = 2,
    IMAPI_FEATURE_PAGE_TYPE_REMOVABLE_MEDIUM = 3,
    IMAPI_FEATURE_PAGE_TYPE_WRITE_PROTECT = 4,
    IMAPI_FEATURE_PAGE_TYPE_RANDOMLY_READABLE = 0x10,
    IMAPI_FEATURE_PAGE_TYPE_CD_MULTIREAD = 0x1d,
    IMAPI_FEATURE_PAGE_TYPE_CD_READ = 0x1e,
    IMAPI_FEATURE_PAGE_TYPE_DVD_READ = 0x1f,
    IMAPI_FEATURE_PAGE_TYPE_RANDOMLY_WRITABLE = 0x20,
    IMAPI_FEATURE_PAGE_TYPE_INCREMENTAL_STREAMING_WRITABLE = 0x21,
    IMAPI_FEATURE_PAGE_TYPE_SECTOR_ERASABLE = 0x22,
    IMAPI_FEATURE_PAGE_TYPE_FORMATTABLE = 0x23,
    IMAPI_FEATURE_PAGE_TYPE_HARDWARE_DEFECT_MANAGEMENT = 0x24,
    IMAPI_FEATURE_PAGE_TYPE_WRITE_ONCE = 0x25,
    IMAPI_FEATURE_PAGE_TYPE_RESTRICTED_OVERWRITE = 0x26,
    IMAPI_FEATURE_PAGE_TYPE_CDRW_CAV_WRITE = 0x27,
    IMAPI_FEATURE_PAGE_TYPE_MRW = 0x28,
    IMAPI_FEATURE_PAGE_TYPE_ENHANCED_DEFECT_REPORTING = 0x29,
    IMAPI_FEATURE_PAGE_TYPE_DVD_PLUS_RW = 0x2a,
    IMAPI_FEATURE_PAGE_TYPE_DVD_PLUS_R = 0x2b,
    IMAPI_FEATURE_PAGE_TYPE_RIGID_RESTRICTED_OVERWRITE = 0x2c,
    IMAPI_FEATURE_PAGE_TYPE_CD_TRACK_AT_ONCE = 0x2d,
    IMAPI_FEATURE_PAGE_TYPE_CD_MASTERING = 0x2e,
    IMAPI_FEATURE_PAGE_TYPE_DVD_DASH_WRITE = 0x2f,
    IMAPI_FEATURE_PAGE_TYPE_DOUBLE_DENSITY_CD_READ = 0x30,
    IMAPI_FEATURE_PAGE_TYPE_DOUBLE_DENSITY_CD_R_WRITE = 0x31,
    IMAPI_FEATURE_PAGE_TYPE_DOUBLE_DENSITY_CD_RW_WRITE = 0x32,
    IMAPI_FEATURE_PAGE_TYPE_LAYER_JUMP_RECORDING = 0x33,
    IMAPI_FEATURE_PAGE_TYPE_CD_RW_MEDIA_WRITE_SUPPORT = 0x37,
    IMAPI_FEATURE_PAGE_TYPE_BD_PSEUDO_OVERWRITE = 0x38,
    IMAPI_FEATURE_PAGE_TYPE_DVD_PLUS_R_DUAL_LAYER = 0x3b,
    IMAPI_FEATURE_PAGE_TYPE_BD_READ = 0x40,
    IMAPI_FEATURE_PAGE_TYPE_BD_WRITE = 0x41,
    IMAPI_FEATURE_PAGE_TYPE_HD_DVD_READ = 0x50,
    IMAPI_FEATURE_PAGE_TYPE_HD_DVD_WRITE = 0x51,
    IMAPI_FEATURE_PAGE_TYPE_POWER_MANAGEMENT = 0x100,
    IMAPI_FEATURE_PAGE_TYPE_SMART = 0x101,
    IMAPI_FEATURE_PAGE_TYPE_EMBEDDED_CHANGER = 0x102,
    IMAPI_FEATURE_PAGE_TYPE_CD_ANALOG_PLAY = 0x103,
    IMAPI_FEATURE_PAGE_TYPE_MICROCODE_UPDATE = 0x104,
    IMAPI_FEATURE_PAGE_TYPE_TIMEOUT = 0x105,
    IMAPI_FEATURE_PAGE_TYPE_DVD_CSS = 0x106,
    IMAPI_FEATURE_PAGE_TYPE_REAL_TIME_STREAMING = 0x107,
    IMAPI_FEATURE_PAGE_TYPE_LOGICAL_UNIT_SERIAL_NUMBER = 0x108,
    IMAPI_FEATURE_PAGE_TYPE_MEDIA_SERIAL_NUMBER = 0x109,
    IMAPI_FEATURE_PAGE_TYPE_DISC_CONTROL_BLOCKS = 0x10a,
    IMAPI_FEATURE_PAGE_TYPE_DVD_CPRM = 0x10b,
    IMAPI_FEATURE_PAGE_TYPE_FIRMWARE_INFORMATION = 0x10c,
    IMAPI_FEATURE_PAGE_TYPE_AACS = 0x10d,
    IMAPI_FEATURE_PAGE_TYPE_VCPS = 0x110
}

根据 MSDN,IMAPI_FEATURE_PAGE_TYPE 不仅可以包含这个值,所以要小心。

于 2015-09-27T06:36:23.513 回答