2005. május 18., szerda

How to get the drive letter of a CD-ROM drive


Problem/Question/Abstract:

I am writing a program which needs the drive name (c , d ,e, ...) of the CD-ROM drive. But that name can be different on different computers. In which file (win.ini , system.ini, registry or something else) can I find the special drive name?

Answer:

Solve 1:

function FindFirstCDROMDrive: Char;
var
  drivemap, mask: DWORD;
  i: Integer;
  root: string;
begin
  Result := #0;
  root := 'A:\';
  drivemap := GetLogicalDrives;
  mask := 1;
  for i := 1 to 32 do
  begin
    if (mask and drivemap) <> 0 then
      if GetDriveType(PChar(root)) = DRIVE_CDROM then
      begin
        Result := root[1];
        Break;
      end;
    mask := mask shl 1;
    Inc(root[1]);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMEssage('First CD drive is ' + FindFirstCDROMDrive);
end;


Solve 2:

function getcdroms: string;
var
  i: Byte;
begin
  Result := '';
  for i := 65 to 90 do
    if GetDriveType(Char(i) + ':\') = DRIVE_CDROM then
      Result := Result + Char(i);
end;


Solve 3:

Use the GetDriveType API function. The following will show all CD-ROM drives, to get only the first one put a 'Break' into the loop.


var
  ch: Char;
  s: string;
begin
  for ch := 'D' to 'Z' do
  begin
    s := ch + ':\';
    if GetDriveType(PChar(s)) = DRIVE_CDROM then
      ShowMessage('CDROM is ' + s[1])
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése