GetFileInformationByHandle Function

Declare Function GetFileInformationByHandle Lib "kernel32.dll" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long

Platforms: Win 32s, Win 95/98, Win NT

GetFileInformationByHandle determines various information about a file. This information includes the file's attributes; its creation, last-access, and last-modified dates and times; the serial number of the disk the file is on; the size of the file; the number of links to it in the file system; and the its unique numeric identifier. All of this information is put into the structure passed as lpFileInformation. The function returns 0 if an error occured, or 1 if successful.

hFile
A handle to the file to get the information of.
lpFileInformation
Receives the information (specified above) relating to the file.

Example:

' Display the serial number of the disk that file C:\MyProgram\datafile.txt
' is on -- in other words, we are finding the serial number of drive C:.  Note that here we
' aren't interested in the other information we receive.  Also note that the alternate declare
' of CreateFile must be used here since we're using Win 95/98 -- see its page for details.
Dim hfile As Long  ' receives the handle to the file
Dim fileinfo As BY_HANDLE_FILE_INFORMATION  ' receives info about the file
Dim hexstring As String  ' will receive the hexadecimal form of the serial number
Dim retval As Long  ' return value

' Get a handle to the file (note how the alternate declare is used):
hfile = CreateFileNS("C:\MyProgram\datafile.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then  ' if the file could not be opened
  Debug.Print "Could not open the file C:\MyProgram\datafile.txt."
  End  ' abort the program
End If

' Display the serial number, using hexadecimal:
retval = GetFileInformationByHandle(hfile, fileinfo)  ' read the information
hexstring = Hex(fileinfo.dwVolumeSerialNumber)  ' get the hexadecimal value of the serial number
If Len(hexstring) < 8 Then  ' if the string is less than 8 characters,
  hexstring = String("0", 8 - Len(hexstring)) & hexstring  ' then right-pad it with "0"s
End If
Debug.Print "The serial number of C: is "; hexstring

' Close the file:
retval = CloseHandle

See Also: GetFileAttributes, GetFileSize, GetFileTime
Category: Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getfileinformationbyhandle.html