Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Checking if a drive is ready for access?

What is the best way to test if a drive is ready for access? This is needed for drives like cd-roms and jaz drives.

Thanks!
0
Kmiller
Asked:
Kmiller
  • 3
  • 3
  • 2
1 Solution
 
philipleighsCommented:
Hi,

Here is a snippet of code that may help.
The important point is the SetErrorMode calls. Without these, (and when running outside Delphi), and when the a: drive is not ready you get a message saying "System error, drive not ready, Abort, Retry, Cancel.
The SetErrorMode supresses these error messages.
You can then call DiskFree. There may be something better than DiskFree, but if it returns -1 then the drive is not ready.

PrevErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
try
   if DiskFree(1) <> -1 then
      //A: Drive is ready
   else
      //A: drive is not ready
finally
  SetErrorMode(PrevErrorMode);
end;

0
 
KmillerAuthor Commented:
how would this be done when you only have the drive letter? Say M:\ which is a cdrom drive.
0
 
philipleighsCommented:
//'A' or 'a' returns 1,
//'B' or 'b' returns 2 etc
function GetDriveNumber(Letter: Char): Integer;
begin
  Result := Ord(UpCase(Letter)) - Ord('A') + 1;
end;


begin
DiskFree(   GetDriveNumber('M')  );
.

Note if you have a string, you could also use GetDriveNumber like this:

s := 'M:\';
DiskFree(   GetDriveNumber(  s[1]  );

Cheers,
Phil.

PS: Tell me if you want code that tells you which drives(s) are CD-ROMs then I'll can paste it here.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dwwangCommented:
This one is from Inprise's FAQ, it does not need to use try..except.

function IsDriveReady(DriveLetter : char) : bool;
                    var
                      OldErrorMode : Word;
                      OldDirectory : string;
                    begin
                      OldErrorMode := SetErrorMode(SEM_NOOPENFILEERRORBOX);
                      GetDir(0, OldDirectory);
                      {$I-}
                        ChDir(DriveLetter + ':\');
                      {$I+}
                       if IoResult <> 0 then
                        Result := False
                       else
                        Result := True;

                      ChDir(OldDirectory);
                      SetErrorMode(OldErrorMode);
                    end;
0
 
KmillerAuthor Commented:
Thanks! They both seem to work... someone answer for credit.
0
 
dwwangCommented:
Sorry for others ^_^
0
 
philipleighsCommented:
Hmm,

Don't you think the first answer is the one that deserves the credit?
0
 
dwwangCommented:
Hi, I just happened to be notified by e-mail and come here to make an answer, if you care of that, now I just ask Kmiller to reject my answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now