Solved

Removable Device

Posted on 2004-11-01
208 Views
Last Modified: 2010-07-27
Hi there,

I wonder if anyone could tell me how to determine if a "drive" is a removable device - such as a USB Memory Key ?
I'm using D6.

DR
0
Question by:DelRunner
    4 Comments
     
    LVL 7

    Expert Comment

    by:sftweng
    Reply-To: "Todd M. Eischeid" <eischeid@erols.com>
    From: "Todd M. Eischeid" <eischeid@erols.com>
    Newsgroups: borland.public.delphi.objectpascal
    References: <b5tmbu864ipn93mofigjaq79aubvft905l@4ax.com> <3cbbca7f$1_1@dnews>
    Subject: Re: How to tell if a drive is Iomega Zip Drive?
    Date: Wed, 17 Apr 2002 17:13:56 -0400
    Lines: 165
    X-Priority: 3
    X-MSMail-Priority: Normal
    X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
    X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
    NNTP-Posting-Host: 208.59.196.112
    Message-ID: <3cbde52e_1@dnews>
    X-Trace: dnews 1019077934 208.59.196.112 (17 Apr 2002 14:12:14 -0700)
    Path: dnews
    Xref: dnews borland.public.delphi.objectpascal:238032

    Eric,

    Below are a set of functions I wrote to Delphi-ize the Win API functions for
    drive information. For Zip drives the type will be dtRemovable and
    TDriveInfo.DiskSize will return a number around 98078 (but only if there is
    a zip disk in the drive; otherwise the DiskSize value will be zero)

    Regards,
    Todd Eischeid

    ********************************

    type TDriveType = (dtUnknown, dtDoesNotExist, dtRemovable, dtFixed,
    dtNetwork, dtCDROM, dtRAMDisk);

    type TDriveInfo = record
       DriveLetter: string;
       DriveType: TDriveType;
       TypeDescrip: string;
       DriveSize: single;  //size in Kilobytes
       DriveFreeSpace: single; //free space in Kilobytes
    end;

    type TDriveInfoArray = array of TDriveInfo;


    //pass a single character string as the 'DriveLetter' param
    function DriveType(DriveLetter: string): TDriveType;
    var
      x,
      DrvType   : Integer;
      DrvLetter,
      DrvString : String;
    begin

        result := dtUnknown;
        DriveLetter := DriveLetter + ':\';

        // get the drive type
        DrvType := GetDriveType(pChar(DriveLetter));

        // set our drive type string accordingly
        case DrvType of
          0                 : result := dtUnknown;
          1                 : result := dtDoesNotExist;
          DRIVE_REMOVABLE  : result := dtRemovable;
          DRIVE_FIXED : result := dtFixed;
          DRIVE_REMOTE : result := dtNetwork;
          DRIVE_CDROM : result := dtCDROM;
          DRIVE_RAMDISK : result := dtRAMDisk;
        end;


    end;


    //returns a listing of all drives and types on the system
    procedure GetAllDriveTypes(var DriveInfoArray: TDriveInfoArray);
    var
      x: integer;

    begin

    DriveInfoArray := nil;
    SetLength(DriveInfoArray, 0);

    //25 possible drives ... a-z
    for x := Ord('A') to Ord('Z') do
    begin
       if not ( DriveType(Chr(x)) = dtDoesNotExist) then
       begin
          SetLength(DriveInfoArray, Length(DriveInfoArray) + 1);
          DriveInfoArray[Length(DriveInfoArray) - 1].DriveLetter := Chr(x);
          DriveInfoArray[Length(DriveInfoArray) - 1].DriveType :=
    DriveType(Chr(x));
          DriveInfoArray[Length(DriveInfoArray) - 1].TypeDescrip :=
    DriveTypeToString(DriveInfoArray[Length(DriveInfoArray) - 1].DriveType);
          DriveInfoArray[Length(DriveInfoArray) - 1].DriveSize := DiskSize(x -
    64) / 1024;  //size in KB
          DriveInfoArray[Length(DriveInfoArray) - 1].DriveFreeSpace :=
    DiskFree(x - 64) / 1024;  //size in KB
       end;


    end;


    end;


    function DriveTypeToString(DriveType: TDriveType): string;
    begin

    case DriveType of
       dtUnknown: result := 'Unknown Drive Type';
       dtDoesNotExist: result := 'Drive Does Not Exist';
       dtRemovable: result := 'Removable Drive';
       dtFixed: result := 'Fixed Drive';
       dtNetwork: result := 'Network Drive';
       dtCDROM: result := 'CD-ROM Drive';
       dtRAMDisk: result := 'RAM Disk Drive';

    end;

    end;



    //Returns true if the drive is ready (useful for removable drives)
    function DriveReady(DriveLetter: string): boolean;
    var
      code: Integer;
      rec: TSearchRec;
      EMode: Word;

    begin

     EMode := SetErrorMode(SEM_FAILCRITICALERRORS);

     DriveLetter := AnsiLowerCase(DriveLetter);

     //FYI,  ord('a') = 97

     try
       result := (DiskSize( Ord(DriveLetter[1]) - 96 ) <> -1)
      finally
       SetErrorMode(EMode);
     end;




    end;



    ***********************************************



    "Eric Jansen" <eric@do.not.spam.keyword.nl> wrote in message
    news:3cbbca7f$1_1@dnews...
    > "Gene Weinbeck" <gene@fundraisersoftware.com> wrote in message
    > news:b5tmbu864ipn93mofigjaq79aubvft905l@4ax.com...
    > > Is there a way to determine if a drive is an Iomega Zip drive?  or any
    > > other high-capacity removable media?
    > >
    >
    > GetDriveType will tell you if the disk is removable, GetDiskFreeSpace(Ex)
    > the disk size.
    >
    > hth
    > --
    > Eric Jansen
    >
    > Find your answers at:
    > Newsgroups archives:   http://groups.google.com
    > Torry's Delphi Pages:   http://www.torry.net
    > Delphi Super Page:   http://delphi.icm.edu.pl
    0
     
    LVL 7

    Accepted Solution

    by:
    0
     
    LVL 1

    Author Comment

    by:DelRunner
    Excelent, workes great - thanks.
    0
     
    LVL 7

    Expert Comment

    by:sftweng
    A pleasure; I'm pleased to have been of help.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now