Exemple #1
0
        public static bool IsAudioCd(string device)
        {
            int colon_idx = device.IndexOf(':');

            if (colon_idx == -1)
            {
                throw new ArgumentException("Invalid device name", "device");
            }

            string filename = string.Concat(@"\\.\", device.Substring(0, colon_idx + 1));

            using (SafeFileHandle file = CreateFile(filename,
                                                    GENERIC_READ,
                                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                    IntPtr.Zero,
                                                    OPEN_EXISTING,
                                                    0,
                                                    IntPtr.Zero)) {
                if (file.IsInvalid)
                {
                    throw new IOException("Opening the CD device failed.");
                }

                uint            ret;
                CDROM_DISK_DATA cdd    = new CDROM_DISK_DATA();
                GCHandle        handle = GCHandle.Alloc(cdd, GCHandleType.Pinned);

                try {
                    if (!DeviceIoControl(file,
                                         IOCTL_CDROM_DISK_TYPE,
                                         IntPtr.Zero,
                                         0,
                                         handle.AddrOfPinnedObject(),
                                         (uint)Marshal.SizeOf(cdd),
                                         out ret,
                                         IntPtr.Zero))
                    {
                        throw new IOException("Error reading disk type");
                    }
                } finally {
                    handle.Free();
                }

                return((cdd.DiskData & 0x03) == CDROM_DISK_AUDIO_TRACK);
            }
        }
Exemple #2
0
        public static bool IsAudioCd(string device)
        {
            int colon_idx = device.IndexOf(':');
            if (colon_idx == -1) {
                throw new ArgumentException("Invalid device name", "device");
            }

            string filename = string.Concat(@"\\.\", device.Substring(0, colon_idx + 1));

            using (SafeFileHandle file = CreateFile(filename,
                                                    GENERIC_READ,
                                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                    IntPtr.Zero,
                                                    OPEN_EXISTING,
                                                    0,
                                                    IntPtr.Zero)) {

                if (file.IsInvalid)
                    throw new IOException("Opening the CD device failed.");

                uint ret;
                CDROM_DISK_DATA cdd = new CDROM_DISK_DATA();
                GCHandle handle = GCHandle.Alloc(cdd, GCHandleType.Pinned);

                try {

                    if (!DeviceIoControl(file,
                                         IOCTL_CDROM_DISK_TYPE,
                                         IntPtr.Zero,
                                         0,
                                         handle.AddrOfPinnedObject(),
                                         (uint)Marshal.SizeOf(cdd),
                                         out ret,
                                         IntPtr.Zero))
                        throw new IOException("Error reading disk type");

                } finally {
                    handle.Free();
                }

                return ((cdd.DiskData & 0x03) == CDROM_DISK_AUDIO_TRACK);
            }
        }