有没有人知道在Windows 2000或更高版本上以编程方式关闭CD托盘的方法?打开CD托盘存在,但我似乎无法让它关闭,尤其是在W2k下.
我特别想找一种方法从批处理文件中执行此操作,如果可能的话,但API调用可以.
我有点喜欢使用DeviceIOControl,因为它可以弹出任何类型的可移动驱动器(例如USB和闪存盘以及CD托盘).使用DeviceIOControl正确弹出磁盘的Da codez(只需添加正确的错误处理):
bool ejectDisk(TCHAR driveLetter) { TCHAR tmp[10]; _stprintf(tmp, _T("\\\\.\\%c:"), driveLetter); HANDLE handle = CreateFile(tmp, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); DWORD bytes = 0; DeviceIoControl(handle, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &bytes, 0); DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &bytes, 0); DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, &bytes, 0); CloseHandle(handle); return true; }
以下是使用Win32 API的简单方法:
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)] protected static extern int mciSendString(string lpstrCommand,StringBuilder lpstrReturnString,int uReturnLength,IntPtr hwndCallback); public void OpenCloseCD(bool Open) { if (Open) { mciSendString("set cdaudio door open", null, 0, IntPtr.Zero); } else { mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero); } }