How to Get Current Drive's Free Disk Space in VB 4.0Last reviewed: May 9, 1997Article ID: Q147686 |
The information in this article applies to:
SUMMARYThis article shows how to find out how much free disk space is available on the current drive by calling the DiskSpaceFree function found in STKIT416.DLL for Win16 or STKIT432.DLL for Win32. Both dynamic-link libraries (DLLs) ship with the Setup kit in Microsoft Visual Basic version 4.0 for Windows. The DiskSpaceFree function is useful for finding the free space available on a drive. If you use the DiskSpaceFree function in your Visual Basic program, you need to distribute STKIT416.DLL or STKIT432.DLL to your customers.
MORE INFORMATIONThere is also a function in the Win32 API (Application Programmers Interface) called GetDiskFreeSpace provided by the Win32 operating system that retrieves information about a specified disk, including the amount of free space on the disk. Refer to Method II in the example below. There are also other functions in the STKIT416.DLL or STKIT432.DLL that may be useful. You can use the AllocUnit function to get the disk allocation unit for the current drive, and use the SetTime function to set the destination file's date and time to the source file's date and time. Examples of using these SETUPKIT functions are also included in the \VB\SETUPKIT\SETUP1\SETUP1.VBP project which is part of the SetupWizard.
Steps to Finding Out How Much Free Space Is Available on Drive C
Command1 Caption Press for Free Space on Drive C Label1 AutoSize True Label2 AutoSize True
#If Win32 Then Private Declare Function DiskSpaceFree Lib "STKIT432.DLL" Alias _ "DISKSPACEFREE" () As Long Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _ "GetDiskFreeSpaceA" ( ByVal lpRootPathName As String, _ lpSectorsPerCluster As Long, lpBytesPerSector As Long, _ lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) _ As Long #Else Private Declare Function DiskSpaceFree Lib "STKIT416.DLL" () As Long #End If
Private Sub Command1_Click() Dim free_Space As Long ChDrive "C:" ' Method I (using the Win16 API). free_Space = DiskSpaceFree() Label1.Caption = "(Method I: ) The total free space on Drive C: = " _ & Str$(free_Space) & " bytes" ' Method II (using the Win32 API). #If Win32 Then Dim numSectorsPerCluster As Long Dim numBytesPerSector As Long Dim numFreeClusters As Long Dim numTotalClusters As Long Dim success As Boolean success = GetDiskFreeSpace("C:\", numSectorsPerCluster, _ numBytesPerSector, numFreeClusters, numTotalClusters) free_Space = numSectorsPerCluster * numBytesPerSector * _ numFreeClusters Label2.Caption = _ "(Method II:) The total free space on Drive C: = " & _ Str$(free_Space) & " bytes" #Else Label2.Caption = "Use Method I for Win16 applications" #End If End Sub |
Keywords : APrgOther vb4all vb4win
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |