Link to home
Start Free TrialLog in
Avatar of hutelihut
hutelihut

asked on

getting Volume serial number

How do I get the volume serial number from any kind of drive (ex. floppydisk or harddrive)?
Avatar of Slavak
Slavak

Use GetVolumeInformation function
ASKER CERTIFIED SOLUTION
Avatar of alanwhincup
alanwhincup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NuttyProfessor Graded me with an A on this link for this above answer
https://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20170016


Here is complete and easy to use function to get HDD serial number:

function GetVolumeID(DriveChar: Char): String;
var
 MaxFileNameLength, VolFlags, SerNum: DWord;
begin
 if GetVolumeInformation(PChar(DriveChar + ':\'), nil, 0,
    @SerNum, MaxFileNameLength, VolFlags, nil, 0)
 then
 begin
   Result := IntToHex(SerNum,8);
   Insert('-', Result, 5);
 end
 else
  Result := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text:=GetVolumeID(extractfilepath(application.exename)[1]);    //This will return the drive where

the program is runnig into (Note: it may not be the C drive!)
end;


This should do it!
VSF
www.victory.hpg.com.br 
Avatar of hutelihut

ASKER

Is'nt it true, that GetVolumeInformation only works on harddrives???
(im not sure..)
(ohh bad gramma, I think - hope you will understand)
No!
It's not true!
I just tested it on my machine using Delphi 5 and Windows 98 and it worked perfectly for my floppy disk!

Take a better look at the code!
Trust me.... it works!
VSF
www.victory.hpg.com.br


function GetVolumeID(DriveChar: Char): String;
var
MaxFileNameLength, VolFlags, SerNum: DWord;
begin
if GetVolumeInformation(PChar(DriveChar + ':\'), nil, 0,
   @SerNum, MaxFileNameLength, VolFlags, nil, 0)
then
begin
  Result := IntToHex(SerNum,8);
  Insert('-', Result, 5);
end
else
 Result := '';
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
button1.caption:=GetVolumeID(edit1.text[1]); //Type A in the edit1 and it will work for sure!
end;
Okey, who should now have the points?
It's up to you to decide which solution fits better!

VSF
www.victory.hpg.com.br
Alanwhincup was the first one who mentioned GetVolumeInformation