Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
485 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Determining if a logical drive existsComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
09/12/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet logical drive unit exists letter existence present valid GetLogicalDrives IsLogicalDrive
Times Scored:
4
Visits:
3640
Uploader: Ernesto De Spirito
Company: Latium Software
Reference: Pascal Newsletter #28
Component Download: http://www.latiumsoftware.com/download/p0028.zip
 
Question/Problem/Abstract:
How to know if there is a drive assigned to a letter.
Answer:



You can use the API GetLogicalDrives to get the logical drives present in the system. This function returns a 32-bit value where the bits represent the logical units. For example:

  +---------------------------------- bit 31 (most significant bit)
  |                              +--- bit 0 (least significant bit)
  |                              |
  00000000000000000000000000101101
        |                 ||||||||
        |                 |||||||+--- 1 ==> Drive A: present
        |                 ||||||+---- 0 ==> Drive B: absent
        |                 |||||+----- 1 ==> Drive C: present
        |                 ||||+------ 1 ==> Drive D: present
        |                 |||+------- 0 ==> Drive E: absent
        |                 ||+-------- 1 ==> Drive F: present
        |                 |+--------- 0 ==> Drive G: absent
        |                 +---------- 0 ==> Drive H: absent
        |                               :   :    :
        +---------------------------- 0 ==> Drive Z: absent

To get the bit mask corresponding to a drive letter in order to test the result of GetLogicalDrives, we can use the following expression:

  1 Shl (Ord(DriveLetter) - Ord('A'))

For example, if DriveLetter was 'D', the result of this expression would be:

  1 shl (Ord('D') - Ord('A'))  =   1 shl (68 - 65)  =  1 shl 3  =  8

In binary:

  00000000000000000000000000001000

A bitwise And between the mask and the result of GetLogicalDrives will be zero if bit 3 isn't set (i.e. if drive D: isn't a valid logical drive).

All right then, let's get to the function:

  uses Windows;

  function IsLogicalDrive(Drive: string): boolean;
  var
    sDrive: string;
    cDrive: char;
  begin
    sDrive := ExtractFileDrive(Drive);
    if sDrive = '' then
      Result := False
    else begin
      cDrive := UpCase(sDrive[1]);
      if cDrive in ['A'..'Z'] then
        result := (GetLogicalDrives And
          (1 Shl (Ord(cDrive) - Ord('A')))) <> 0
      else
        Result := False;
    end;
  end;

Sample call:

  if not IsLogicalDrive(Edit1.Text) then
    ShowMessage(Format('"%s" is not a valid drive.',
      [ExtractFileDrive(Edit1.Text)]));





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)