De otro modo podria ser así:
En este caso cada 15 minutos abrira y cerrara el cd rom por un minuto, el Interval del timer = 3000.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("kernel32")]
static extern bool DeviceIoControl(IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("kernel32")]
static extern IntPtr CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32")]
static extern int CloseHandle(IntPtr dHandle);
private static IntPtr fHandle;
private static uint ret;
public Form1()
{
InitializeComponent();
}
private void cdrom()
{
DriveInfo[] aDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in aDrives)
{
if (d.DriveType.ToString().ToUpper() == "CDROM")
{
abrir (d.Name.Remove (2));
cerrar (d.Name.Remove (2));
}
}
}
private void abrir(string cd)
{
fHandle = CreateFile(@"\\?\" + cd, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
DeviceIoControl(fHandle, 0x2D4808, IntPtr.Zero, 0, IntPtr.Zero, 0, ref ret, IntPtr.Zero);
CloseHandle(fHandle);
fHandle = IntPtr.Zero;
}
private void cerrar(string cd)
{
fHandle = CreateFile(@"\\?\" + cd, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
DeviceIoControl(fHandle, 0x2D480C, IntPtr.Zero, 0, IntPtr.Zero, 0, ref ret, IntPtr.Zero);
CloseHandle(fHandle);
fHandle = IntPtr.Zero;
}
private void timer1_Tick(object sender, EventArgs e)
{
switch (DateTime.Now.Minute)
{
case 00:
cdrom();
break;
case 15:
cdrom();
break;
case 30:
cdrom();
break;
case 45:
cdrom();
break;
}
}
}
}
En caso de unidades virtuales de cd rom necesitaria mejorar el codigo para buen funcionamiento.