Solved

How to get drive letter/volume label from all connected drives

Posted on 2008-06-20
2
9,591 Views
Last Modified: 2012-06-22
Gents,

I already use the following function to get the volume label using a specific drive letter. I need to be able to scan each connected drive and call this function for each drive. (Unless there is a better way)

Project description:

I am building a video player that works with a database. When I want to play a file, I look in the database to know on which drive the video is stored.

Each drive has got a specific volume label such as BB1, BB2, BB3, ect.

I have built a 4 bay hot swappable HDD USB reader.

Lets say I want to play a file which is stored on BB7.

I need to look through the present HDDs in my reader to see if BB7 is in. If so, play the file, else prompt the user to insert BB7.

In my database, the file name is stored in a string "\Video\myfile.mp2" so I only need to add the right drive letter to the begining of the string in order to pass to my player.

I am not too keen in scanning drive letters from D to Z to look for the label. I would like a cleaner solution, scan through connected drives...

Many thanks.


function GetVolumeLabel(DriveChar: Char): string;

var

  NotUsed:     DWORD;

  VolumeFlags: DWORD;

  VolumeInfo:  array[0..MAX_PATH] of Char;

  VolumeSerialNumber: DWORD;

  Buf: array [0..MAX_PATH] of Char;

begin

    GetVolumeInformation(PChar(DriveChar + ':\'),

    Buf, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,

    VolumeFlags, nil, 0);
 

    SetString(Result, Buf, StrLen(Buf));   { Set return result }

    Result:=AnsiUpperCase(Result)

end;

Open in new window

0
Comment
Question by:etiennedemers
2 Comments
 
LVL 13

Accepted Solution

by:
ThievingSix earned 500 total points
ID: 21832013
I know you didn't ask for scanning through all drives, but it takes less time than GetTickCount to update..
function GetVolumeLabel(DriveChar: Char): string;

var

  NotUsed:     DWORD;

  VolumeFlags: DWORD;

  VolumeInfo:  array[0..MAX_PATH] of Char;

  VolumeSerialNumber: DWORD;

  Buf: array [0..MAX_PATH] of Char;

begin

    GetVolumeInformation(PChar(DriveChar + ':\'),

    Buf, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,

    VolumeFlags, nil, 0);

 

    SetString(Result, Buf, StrLen(Buf));   { Set return result }

    Result:=AnsiUpperCase(Result)

end;
 

procedure TForm1.Button1Click(Sender: TObject);

var

  Drive : Char;

  DriveLetter : String;

begin

  For Drive := 'A' To 'Z' Do

    begin

    DriveLetter := Drive + ':\';

    If GetDriveType(PChar(DriveLetter)) = DRIVE_FIXED Then

      begin

      Memo1.Lines.Add(Drive + ': ' + GetVolumeLabel(Drive));

    end;

  end;

end;

Open in new window

0
 

Author Closing Comment

by:etiennedemers
ID: 31469162
This will achieve my goals.

Many thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System restore point 4 91
Machine not responding during CopyFile() 3 89
Path  to current project in Delphi. 2 72
Internet Explorer View Settings Question 15 106
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

16 Experts available now in Live!

Get 1:1 Help Now