How I can detect hardware?

How I can detect a cd-rw or dvd-rw,etc. using Visual C#


Answer this question

How I can detect hardware?

  • Hugo Kornelis

    thanks.....very much......

  • sdallamura

    Jonny,
    sorry for the long post that follows. If you need to address issues about burning CD's and DVD's, you may find some interesting information here, where it's mentioned the ICDBurn interface. Quite interesting.

    If you "only" want to detect the actual capabilities of your drives, and what you get via DriveType (DriveInfo.DriveType), you will have to use the DeviceIoControl API that will get you all the information you want, and maybe some more. As usual,you won't get away with a single call. You need to set up some structures, enums, and all also CreateFile and CloseHandle. What follows is a crude example (it looks huge only because there are all the enum's) that you will have to adapt to your needs.

    public class DriveInfoEx {

    const int IOCTL_STORAGE_GET_MEDIA_TYPES_EX = (0x0000002d << 16) | (0x0301 << 2);

    const uint AccessReadWrite = 0xc0000000;

    const uint ShareReadWrite = 3;

    const uint OpenExisting = 3;

    [DllImport ("kernel32.dll")]

    private static extern bool DeviceIoControl (IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, ref GET_MEDIA_TYPES lpOutBuffer, int nOutBufferSize, ref UInt32 lpBytesReturned, IntPtr lpOverlapped);

    [DllImport ("kernel32.dll")]

    private static extern IntPtr CloseHandle (IntPtr hObject);

    [DllImport ("kernel32.dll")]

    private static extern IntPtr CreateFile (string lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplate);

    private struct DEVICE_MEDIA_INFO {

    public Int64 Cylinders;

    public StorageMediaType MediaType;

    public UInt32 TracksPerCylinder;

    public UInt32 SectorsPerTrack;

    public UInt32 BytesPerSector;

    public UInt32 NumberMediaSides;

    public MediaCharacteristics MediaCharacteristics;

    }

    private struct GET_MEDIA_TYPES {

    public DeviceType DeviceType;

    public UInt32 MediaInfoCount;

    [MarshalAs (UnmanagedType.ByValArray, SizeConst=50)]

    public DEVICE_MEDIA_INFO [] MediaInfo;

    }

    public enum DeviceType : uint {

    Beep = 1,

    CDRom,

    CDRomFileSystem,

    DeviceController,

    DataLink,

    Dfs,

    Disk,

    DiskFileSystem,

    Inport,

    Keyboard,

    Mailslot,

    MidiIn,

    MidiOut,

    Mouse,

    MultiUncProvider,

    NamedPipe,

    Network,

    NetworkBrowser,

    NetworkFileSystem,

    Null,

    ParallelPort,

    PhysicalNetCard,

    Printer,

    Scanner,

    SerialMousePort,

    SerialPort,

    Screen,

    Sound,

    Streams,

    Tape,

    TapeFileSystem,

    Transport,

    Unknown,

    Video,

    VirtualDisk,

    WaveIn,

    WaveOut,

    Port8042,

    NetworkRedirector,

    Battery,

    BusExtender,

    Modem,

    Vdm,

    MassStorage,

    Smb,

    KS,

    Changer,

    Acpi,

    Dvd,

    FullscreenVideo,

    DfsFileSystem,

    DfsVolume,

    Serenum,

    TermSrv,

    KSec,

    Fips,

    Infiniband

    }

    [Flags]

    public enum MediaCharacteristics : uint {

    None = 0,

    Erasable = 0x00000001,

    WriteOnce = 0x00000002,

    ReadOnly = 0x00000004,

    ReadWrite = 0x00000008,

    WriteProtected = 0x00000100,

    Mounted = 0x80000000

    }

    public enum StorageMediaType : uint {

    Unknown = 0,

    F5_1Pt2_512, // 5.25", 1.2MB, 512 bytes/sector

    F3_1Pt44_512, // 3.5", 1.44MB, 512 bytes/sector

    F3_2Pt88_512, // 3.5", 2.88MB, 512 bytes/sector

    F3_20Pt8_512, // 3.5", 20.8MB, 512 bytes/sector

    F3_720_512, // 3.5", 720KB, 512 bytes/sector

    F5_360_512, // 5.25", 360KB, 512 bytes/sector

    F5_320_512, // 5.25", 320KB, 512 bytes/sector

    F5_320_1024, // 5.25", 320KB, 1024 bytes/sector

    F5_180_512, // 5.25", 180KB, 512 bytes/sector

    F5_160_512, // 5.25", 160KB, 512 bytes/sector

    RemovableMedia, // Removable media other than floppy

    FixedMedia, // Fixed hard disk media

    F3_120M_512, // 3.5", 120M Floppy

    F3_640_512, // 3.5" , 640KB, 512 bytes/sector

    F5_640_512, // 5.25", 640KB, 512 bytes/sector

    F5_720_512, // 5.25", 720KB, 512 bytes/sector

    F3_1Pt2_512, // 3.5" , 1.2Mb, 512 bytes/sector

    F3_1Pt23_1024, // 3.5" , 1.23Mb, 1024 bytes/sector

    F5_1Pt23_1024, // 5.25", 1.23MB, 1024 bytes/sector

    F3_128Mb_512, // 3.5" MO 128Mb 512 bytes/sector

    F3_230Mb_512, // 3.5" MO 230Mb 512 bytes/sector

    F8_256_128, // 8", 256KB, 128 bytes/sector

    F3_200Mb_512, // 3.5", 200M Floppy (HiFD)

    //

    DDS_4mm = 0x20, // Tape - DAT DDS1,2,... (all vendors)

    MiniQic, // Tape - miniQIC Tape

    Travan, // Tape - Travan TR-1,2,3,...

    QIC, // Tape - QIC

    MP_8mm, // Tape - 8mm Exabyte Metal Particle

    AME_8mm, // Tape - 8mm Exabyte AME

    AIT1_8mm, // Tape - 8mm Sony AIT

    DLT, // Tape - DLT Compact IIIxt, IV

    NCTP, // Tape - Philips NCTP

    IBM_3480, // Tape - IBM 3480

    IBM_3490E, // Tape - IBM 3490E

    IBM_Magstar_3590, // Tape - IBM Magstar 3590

    IBM_Magstar_MP, // Tape - IBM Magstar MP

    STK_DATA_D3, // Tape - STK Data D3

    SONY_DTF, // Tape - Sony DTF

    DV_6mm, // Tape - 6mm Digital Video

    DMI, // Tape - Exabyte DMI and compatibles

    SONY_D2, // Tape - Sony D2S and D2L

    CLEANER_CARTRIDGE, // Cleaner

    CD_ROM, // Opt_Disk - CD

    CD_R, // Opt_Disk - CD-Recordable

    CD_RW, // Opt_Disk - CD-Rewriteable

    DVD_ROM, // Opt_Disk - DVD-ROM

    DVD_R, // Opt_Disk - DVD-Recordable

    DVD_RW, // Opt_Disk - DVD-Rewriteable

    MO_3_RW, // Opt_Disk - 3.5" Rewriteable MO Disk

    MO_5_WO, // Opt_Disk - MO 5.25" Write Once

    MO_5_RW, // Opt_Disk - MO 5.25" Rewriteable (not LIMDOW)

    MO_5_LIMDOW, // Opt_Disk - MO 5.25" Rewriteable (LIMDOW)

    PC_5_WO, // Opt_Disk - Phase Change 5.25" Write Once Optical

    PC_5_RW, // Opt_Disk - Phase Change 5.25" Rewriteable

    PD_5_RW, // Opt_Disk - PhaseChange Dual Rewriteable

    ABL_5_WO, // Opt_Disk - Ablative 5.25" Write Once Optical

    PINNACLE_APEX_5_RW, // Opt_Disk - Pinnacle Apex 4.6GB Rewriteable Optical

    SONY_12_WO, // Opt_Disk - Sony 12" Write Once

    PHILIPS_12_WO, // Opt_Disk - Philips/LMS 12" Write Once

    HITACHI_12_WO, // Opt_Disk - Hitachi 12" Write Once

    CYGNET_12_WO, // Opt_Disk - Cygnet/ATG 12" Write Once

    KODAK_14_WO, // Opt_Disk - Kodak 14" Write Once

    MO_NFR_525, // Opt_Disk - Near Field Recording (Terastor)

    NIKON_12_RW, // Opt_Disk - Nikon 12" Rewriteable

    IOMEGA_ZIP, // Mag_Disk - Iomega Zip

    IOMEGA_JAZ, // Mag_Disk - Iomega Jaz

    SYQUEST_EZ135, // Mag_Disk - Syquest EZ135

    SYQUEST_EZFLYER, // Mag_Disk - Syquest EzFlyer

    SYQUEST_SYJET, // Mag_Disk - Syquest SyJet

    AVATAR_F2, // Mag_Disk - 2.5" Floppy

    MP2_8mm, // Tape - 8mm Hitachi

    DST_S, // Ampex DST Small Tapes

    DST_M, // Ampex DST Medium Tapes

    DST_L, // Ampex DST Large Tapes

    VXATape_1, // Ecrix 8mm Tape

    VXATape_2, // Ecrix 8mm Tape

    STK_9840, // STK 9840

    LTO_Ultrium, // IBM, HP, Seagate LTO Ultrium

    LTO_Accelis, // IBM, HP, Seagate LTO Accelis

    DVD_RAM, // Opt_Disk - DVD-RAM

    AIT_8mm, // AIT2 or higher

    ADR_1, // OnStream ADR Mediatypes

    ADR_2,

    STK_9940 // STK 9940

    }

    public static void CheckCapabilities (string drive) {

    string drivePath = @"\\.\" + drive.Replace (@"\", "");

    IntPtr hDrive = CreateFile (drivePath, AccessReadWrite, ShareReadWrite, IntPtr.Zero, OpenExisting, 0, IntPtr.Zero);

    if ((int) hDrive != -1) {

    try {

    GET_MEDIA_TYPES media = new GET_MEDIA_TYPES ();

    UInt32 returned = 0;

    if (DeviceIoControl (hDrive, IOCTL_STORAGE_GET_MEDIA_TYPES_EX, IntPtr.Zero, 0, ref media, Marshal.SizeOf (media), ref returned, IntPtr.Zero)) {

    // inspect drive only

    bool isDvdDrive = (media.DeviceType == DeviceType.Dvd);

    bool isCDDrive = (media.DeviceType == DeviceType.CDRom);

    // inspect all supported media

    for (int i = 0; i < media.MediaInfoCount; i++) {

    Console.WriteLine ("drive {0} supports {1}, {2}", drive, media.MediaInfo Idea.MediaType.ToString (), media.MediaInfo Idea.MediaCharacteristics.ToString ());

    }

    } else {

    throw new Exception ("DeviceIoControl failed: " + Marshal.GetLastWin32Error ().ToString ());

    }

    } finally {

    CloseHandle (hDrive);

    }

    } else

    throw new Exception ("CreateFile failed: " + Marshal.GetLastWin32Error ().ToString ());

    }

    }

    As I don't have a DVD-RW on hand at the moment, I don't know if it will work for you, but it should be a starting point. My DVD/CD is correctly reported as a DVD Drive (through the DeviceType), but it insists it only supports CD-ROM and Removable Device (the latter as Read/Write). Also, it doesn't seem to work with floppy drive.

    HTH
    --mc


  • imidotnet

    PS: I wan't to create an application that display the model name of an cdrom or cd-rw or,,,

  • MSGTSC_Test

    I dont think you can really display the model name as such in C# as this would require you to go low level.

    you could again maybe use Win32 API calls, check msdn for this. Best way would probably to get devices from device manager, if there is a way to access it via API



  • CityOfOrlando

    in .NET 2.0 you can use the System.IO namespace.

    In this, you can use the DriveInfo class and you can then go through the collection of drives returned and get its information you require.

     

    Example:

     



    System.IO.DriveInfo[] theDrives = System.IO.DriveInfo.GetDrives();
    foreach (DriveInfo currentDrive in theDrives)
    {
       MessageBox.Show("DriveName: " + currentDrive.Name + Environment.NewLine + "Drive Type: " + currentDrive.DriveType.ToString() + Environment.NewLine + "Drive Format: " + currentDrive.DriveFormat);
    }

     



  • mary_l

    ok.it works.....but what is the result For example the device name........(I talk about the last result, the first is not working: error: "The device is not ready")

  • Cool Programmer

    with what I replace and DLLImport is a reference how I introduce it

  • How I can detect hardware?