Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4240354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:09:13+00:00 2026-05-21T03:09:13+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:09:14+00:00Added an answer on May 21, 2026 at 3:09 am

    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;
             }
      }
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: C# - Get list of open tasks On Windows XP, how do
Possible Duplicate: .NET windows application, can it be compressed into a single .exe? To
Possible Duplicates: Best IDE / TextEditor for R Recommendations for Windows text editor for
Possible Duplicates: iPhone development on PC iPhone development on Windows Do I really need
Possible Duplicates: Best “official” scripting language for Windows programmers Universal scripting language for Windows
Possible Duplicates: free matlab environment What’s the best MATLAB equivalent? (open source or otherwise
Possible Duplicates: How can I get the size of an array from a pointer
Possible Duplicates: Determine if O/S is Windows 7… How to tell if the OS
Possible Duplicate: How to detect Windows 64 bit platform with .net? How can I
Possible Duplicates: Detect Antivirus on Windows using C# How to detect if a virusscanner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.