Abrir y cerrar bandeja.
Meta:
Hello.
Trabajo con el Visual C# Express 2008. ¿Se puede hacer con un Firm1 y dos button abrir y cerrar la bandeja de un lector de disco del PC?
Si es posible. ¿Cómo se hace?
Adiós.
Myth.ck:
Hola meta te refieres a esto?:
Código
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace EjectMedia
{
class Program
{
static void Main(string[] args)
{
// My CDROM is on drive E:
EjectMedia.Eject(@"\\.\E:");
}
}
class EjectMedia
{
// Constants used in DLL methods
const uint GENERICREAD = 0x80000000;
const uint OPENEXISTING = 3;
const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
const int INVALID_HANDLE = -1;
// File Handle
private static IntPtr fileHandle;
private static uint returnedBytes;
// Use Kernel32 via interop to access required methods
// Get a File Handle
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string fileName,
uint desiredAccess,
uint shareMode,
IntPtr attributes,
uint creationDisposition,
uint flagsAndAttributes,
IntPtr templateFile);
[DllImport("kernel32", SetLastError=true)]
static extern int CloseHandle(IntPtr driveHandle);
[DllImport("kernel32", SetLastError = true)]
static extern bool DeviceIoControl(IntPtr driveHandle,
uint IoControlCode,
IntPtr lpInBuffer,
uint inBufferSize,
IntPtr lpOutBuffer,
uint outBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);
public static void Eject(string driveLetter)
{
try
{
// Create an handle to the drive
fileHandle = CreateFile(driveLetter,
GENERICREAD,
0,
IntPtr.Zero,
OPENEXISTING,
0,
IntPtr.Zero);
if ((int)fileHandle != INVALID_HANDLE)
{
// Eject the disk
DeviceIoControl(fileHandle,
IOCTL_STORAGE_EJECT_MEDIA,
IntPtr.Zero, 0,
IntPtr.Zero, 0,
ref returnedBytes,
IntPtr.Zero);
}
}
catch
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
finally
{
// Close Drive Handle
CloseHandle(fileHandle);
fileHandle = IntPtr.Zero;
}
}
}
}
Myth.ck:
O tambien a este ejemplo:
http://www.codeproject.com/KB/cs/workingcd.aspx
Salu2!
Meta:
Gracias, dejo el código más simple.
El primer código que me pusiste lo veo muy grande y complejo.
Código
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;
namespace CD_Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
public static extern void mciSendStringA(string lpstrCommand,
string lpstrReturnString, long uReturnLength, long hwndCallback);
//Why did i put this here?
string rt = "";
private void button1_Click(object sender, EventArgs e)
{
mciSendStringA("set CDAudio door open PROBANDO", rt, 127, 0);
}
private void button2_Click(object sender, EventArgs e)
{
mciSendStringA("set CDAudio door closed PROBANDO", rt, 127, 0);
}
}
}
Saludo.
Myth.ck:
De nada meta.
Salu2!
Navegación
[#] Página Siguiente