Page 1 of 1

How to read the volume label of hard disk?

Posted: Mon May 16, 2011 7:58 pm
by AlanFoo
Hope someone can help.

I need to read the name of the hard disk volume label using Purebasic. How to do it ?


thanks.

Alan

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:06 pm
by ts-soft

Code: Select all

Define lpRootPathName.s = "C:\"
Define lpVolumeNameBuffer.s = Space(#MAX_PATH)
Define nVolumeNameSize = #MAX_PATH

If GetVolumeInformation_(lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize, 0, 0, 0, 0, 0)
  Debug lpVolumeNameBuffer
EndIf
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Greetings - Thomas

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:16 pm
by AlanFoo
Thanks Thomas for responding so fast.


I copied and pasted to the Purebasic editor opening up a new page and tried it.


Define lpRootPathName.s = "C:\"
Define lpVolumeNameBuffer.s = Space(#MAX_PATH)
Define nVolumeNameSize = #MAX_PATH

If GetVolumeInformation_(lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize, 0, 0, 0, 0, 0)
Debug lpVolumeNameBuffer
Debug nVolumeNameSize
Debug lpRootPathName
EndIf

the lpVoumeNameBuffer seems to be blank

The result at the Debug output.

Blank SPACE
260
C:\

Any idea?

thanks
Alan

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:34 pm
by ts-soft
Your Drive have not a volume-name (is empty)

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:40 pm
by AlanFoo
Hi Thomas, ya .. you are right. Went to DOS and dir it - stated no Label :>)

How about reading the "Volume Serial Number"?

Thanks

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:53 pm
by ts-soft
AlanFoo wrote:How about reading the "Volume Serial Number"?
The same API, here with some beautification:

Code: Select all

Procedure.s GetDriveSerial(Drive.s)
  Protected lpVolumeNameBuffer.s, lpVolumeSerialNumber.l
  If Len(Drive) = 1 : Drive + ":\" : EndIf
  If Right(Drive, 1) <> "\" : Drive + "\" : EndIf
  lpVolumeNameBuffer.s = Space(#MAX_PATH +1)
  GetVolumeInformation_(@Drive, @lpVolumeNameBuffer, #MAX_PATH +1, @lpVolumeSerialNumber, 0,0,0,0)
  ProcedureReturn Hex(PeekW(@lpVolumeSerialNumber + 2) & $FFFF) + "-" + Hex(PeekW(@lpVolumeSerialNumber) & $FFFF)
EndProcedure

Re: How to read the volume label of hard disk?

Posted: Mon May 16, 2011 8:59 pm
by AlanFoo
Dear Thomas,

Thanks a million....

Got it.

Warmest regards
Alan

Re: How to read the volume label of hard disk?

Posted: Thu Aug 23, 2012 7:06 pm
by mesozorn
I wonder if I could resurrect this thread to amend it with a related question:

When using GetVolumeInformation on a local disk/drive, the volume label is returned correctly, no problem. But with a mapped network drive, the same API command will successfully return the "true" volume label as mirrored if one right-clicks on the drive in Windows Explorer and selects "Properties". However, I do not know of a way to get its "local" name that is shown in the Explorer tree right beside the drive letter.

Example, my drive letter tree looks like this:

C:\ (System)
D:\ (Applications)
H:\ (Storage) <----- This is a mapped network drive

Drive "H" shows with the name "Storage" in my drive tree, and if I like I can rename this to anything I want and it will be reflected on MY local drive tree, but if I right click on "H" and select "Properties", the volume label shows up as something else, for example "Data drive". And this stays constant no matter what renaming operations I perform locally.

When I use GetVolumeInformation, I am only ever able to retrieve that static unchanging "Data drive" label, presumably set by a network administrator somewhere. Is there any way I can retrieve the "local" drive label, either using WinAPI or a native PB command?

Thanks!