如何弹出 CD-ROM

发布于 2024-10-29 20:30:26 字数 411 浏览 4 评论 0原文

可能的重复:
Windows CDROM 弹出
使用 Windows API 调用打开 CD/DVD 门?

我环顾四周,找不到一个简单的解决方案来完成我想做的事情。

我想从我的 C# 应用程序打开 CD-Rom。它应该检查该媒体是否确实是 CD-ROM,然后将其打开。有没有快速的解决方案或者我错过了什么?

Possible Duplicates:
Windows CDROM Eject
Open CD/DVD door with a Windows API call?

I have looked around and can't find a simple solution to what I want to do.

I want to open a CD-Rom from my C# app. It should check if the media is in fact a cd- rom and then open it. Is there a quick solution to this or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-11-05 20:30:26

检查此 URL,它包含 .net 的托管和非托管代码

http://bytes.com/topic/c-sharp/answers/273513-how-eject-cd-rom-c

尝试以下代码:

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;
         }
  }
 }
}

Check this URL, it has both managed and unmanaged code for .net

http://bytes.com/topic/c-sharp/answers/273513-how-eject-cd-rom-c

Try below code :

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;
         }
  }
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文