Option Explicit ' ' This Visual Basic 6.0 example illustrates how file operations work ' ' Const MOVEFILE_REPLACE_EXISTING = &H1 Const FILE_ATTRIBUTE_TEMPORARY = &H100 Private Const CREATE_NEW As Long = 1 Private Const CREATE_ALWAYS As Long = 2 Private Const OPEN_EXISTING As Long = 3 Private Const OPEN_ALWAYS As Long = 4 Private Const OPEN_IF_EXISTS As Long = (&H1) Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const FILE_BEGIN As Long = 0 Private Const FILE_END As Long = 2 Private Const FILE_SHARE_READ As Long = &H1 Private Const FILE_SHARE_WRITE As Long = &H2 Private Const GENERIC_WRITE As Long = &H40000000 Private Const GENERIC_READ As Long = &H80000000 Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long Private Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long Private Sub Form_Load() Dim VolumeLabel As String Dim FileSystemName As String Dim VolumeSerial As Long Dim MaximumComponent As Long Dim FileSystemFlags As Long Dim Buffer(1 To 100) As Byte Dim StringBuffer As String Dim FileName As String Dim FileHandle As Long Dim NewFileName As String Dim i As Long Dim ret As Long Dim FSizeHigh As Long VolumeLabel = String(255, Chr(0)) FileSystemName = String(255, Chr(0)) ret = GetVolumeInformation("C:\", VolumeLabel, 255, VolumeSerial, 0, 0, FileSystemName, 100) VolumeLabel = Left$(VolumeLabel, InStr(1, VolumeLabel, Chr$(0)) - 1) FileSystemName = Left$(FileSystemName, InStr(1, FileSystemName, Chr$(0)) - 1) MsgBox "The volume label is " & VolumeLabel MsgBox "The volume file system is " & FileSystemName MsgBox "The volume serial number is " & Hex(VolumeSerial) SetVolumeLabel "c:\", "API Label" MsgBox "The volume label of ""C:\"" should now be set to ""API Label"". Click OK and label will be restored." SetVolumeLabel "C:\", VolumeLabel MsgBox "The volume label of ""C:\"" is now set to the old one." FileName = String(255, Chr(0)) GetTempFileName "C:\", "TMP", 0, FileName FileName = Left$(FileName, InStr(1, FileName, Chr$(0)) - 1) MsgBox "The temporary file that will be created is : " & FileName ' Create the file, we're using create_always , create_new would have been ok too because ' GetTempFileName retrieves an unique file name anyway FileHandle = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, CREATE_ALWAYS, 0&, 0&) If FileHandle = INVALID_HANDLE_VALUE Then MsgBox "There was an error creating the file." Exit Sub End If ' Writing text on the file. We could have used a string directly but using a buffer is nicer StringBuffer = "API Secrets test to be written in the file" For i = 1 To Len(StringBuffer) Buffer(i) = Asc(Mid$(StringBuffer, i, 1)) Next i WriteFile FileHandle, Buffer(1), Len(StringBuffer), ret, 0& MsgBox CStr(ret) & " bytes written in the file." CloseHandle FileHandle ' We're closing the file because we need to open it again using GENERIC_READ to read from it. FileHandle = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&) If FileHandle = INVALID_HANDLE_VALUE Then MsgBox "There was an error opening the file" Exit Sub End If SetFilePointer FileHandle, 10, 0, FILE_BEGIN MsgBox "Moved to the 10th byte in the file" ReadFile FileHandle, Buffer(1), 15, ret, 0& MsgBox CStr(ret) & " bytes read from file." StringBuffer = "" For i = 1 To ret StringBuffer = StringBuffer & Chr(Buffer(i)) Next i MsgBox "The data read from file is """ & StringBuffer & """" ' Get File Size must be used on an file opened with CreateFile ' FSizeHigh is used only if file size exceeds the maximum value possible for Long ret = GetFileSize(FileHandle, FSizeHigh) MsgBox "The file size is " & CStr(ret) & " bytes." CloseHandle FileHandle SetFileAttributes FileName, FILE_ATTRIBUTE_TEMPORARY MsgBox "The file attributes is set to Temporary." NewFileName = "C:\ReallyTemporaryFileMovedByDemo.dat" MoveFileEx FileName, NewFileName, MOVEFILE_REPLACE_EXISTING MsgBox "File renamed to " & NewFileName DeleteFile NewFileName MsgBox "File deleted." End Sub