BenEskew.com Just another web developer's personal weblog.

11Sep/090

Visual Basic Code Snippets #7

In this next update I'll continue to re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to System Functions.

Get Free Disk Space on Hard Drives

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long

Public Type DiskInformation
lpSectorsPerCluster As Long lpBytesPerSector As Long
lpNumberOfFreeClusters As Long
lpTotalNumberOfClusters As Long
End Type

Add the following code wherever you want the code to execute...

Dim info As DiskInformation
Dim lAnswer As Long
Dim lpRootPathName As String
Dim lpSectorsPerCluster As Long
Dim lpBytesPerSector As Long
Dim lpNumberOfFreeClusters As Long
Dim lpTotalNumberOfClusters As Long
Dim lBytesPerCluster As Long
Dim lNumFreeBytes As Double
Dim sString As String

lpRootPathName = "c:\" 'simply replace this with your hard drive path
lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
sString = "Number of Free Bytes : " & lNumFreeBytes & vbCr & vbLf
sString = sString & "Number of Free Kilobytes: " & (lNumFreeBytes / 1024) & "K" & vbCr & vbLf
sString = sString & "Number of Free Megabytes: " & Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"
MsgBox sString

Get Windows Directory using API Call

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Const MAX_PATH = 260

Add the following code wherever you want the code to execute...

Dim strBuffer As String
Dim lngReturn As Long
Dim strWindowsDirectory As String
strBuffer = Space$(MAX_PATH)
lngReturn = GetWindowsDirectory(strBuffer, MAX_PATH)
strWindowsDirectory = Left$(strBuffer, Len(strBuffer) - 1)

Clear/Add-to Windows Recent Document List

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Declare Sub SHAddToRecentDocs Lib "shell32.dll" (ByVal uFlags As Long, ByVal pv As String)

Add the following code wherever you want the Clear Recent Document List code to execute...

'clear the entire list
Call SHAddToRecentDocs(2,vbNullString)

Add the following code wherever you want to add the file to the list code to execute...

'add a new file to list
Dim strNewFile as String
strNewFile="c:\myfile.ext"
Call SHAddToRecentDocs(2,strNewFile)

Visual Basic Code Snippets #8