17

I have a DVD reading and burning app in C#. I want to be able to detect the difference between an empty drive (no disk) and an open drive.

Edit: After some more googling, I think a better problem description is "I want to access the drive tray status - open or closed"

I can use

ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DriveType= 5")

to find all the dvd drives in the system, and the properties tell me if there is a disk in the drive. Access property tells me if there is a blank disk.

But Access is null when the drive is open, and when the drive is shut and empty. And I don't see any other property that changes when the drive opens or closes.

Anyone know a way to distinguish an open drive from a closed and empty drive?

I am using Win7, but ideally the solution would work on XP and Vista also.

Kara
  • 6,115
  • 16
  • 50
  • 57
Sugrue
  • 3,629
  • 5
  • 35
  • 53

2 Answers2

5

MediaLoaded property (Win32_CDROMDrive) will tell you whether you have a disc loaded. I am bit confused what you are looking for:

  1. disk tray open
  2. disk tray closed - no media
  3. disk tray closed - has media

MediaLoaded property will help in case 2 (False) and 3 (True)

Lokeshwer
  • 1,139
  • 1
  • 9
  • 14
1

IOCTL_DISK_GET_LENGTH_INFO may be of use to you.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • Thanks for the reply. I'll try, once I figure out how in c#. But from reading it sounds like this param will tell me the size of a drive - I don't understand how it could tell me whether a DVD drive is open or empty? – Sugrue Jun 14 '11 at 11:40
  • Not sure if Stijn's solution is best, but reading the docs you could handle the error condition: If the operation fails or is pending, DeviceIoControl returns zero. To get extended error information, call GetLastError. – Armbrat Jun 14 '11 at 11:47
  • @Sugrue I can't try it out right now, but I assume the sizes of an empty drive (0 or null) and an open drive (700MB or a different value) will be different. – user247702 Jun 14 '11 at 11:52