Calling Win32 API Functions in VB 2010 - Listing 1

'No external references are needed to access the Win32 API

Public Class DiskInfo
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
        Call DiskInfo()
    End Sub

    Private Sub DiskInfo()
        Dim RootPath As String = "C:\"
        Dim SectorsInCluster As Integer = 0
        Dim BytesInSector As Integer = 0
        Dim NumberFreeClusters = 0
        Dim TotalNumberClusters = 0
        Call Win32API.GetDiskFreeSpace(RootPath, SectorsInCluster, BytesInSector, 
NumberFreeClusters, TotalNumberClusters)
MessageBox.Show(String.Format("GetDiskSpace: Free Cluster Count in C: is {0}", NumberFreeClusters))

        Dim FreeBytes As Integer = 0
        Dim TotalBytes As Integer = 0
        Dim TotalFreeBytes As UInt32 = 0
        Call Win32API.GetDiskFreeSpaceEx(RootPath, FreeBytes, TotalBytes, TotalFreeBytes)
        MessageBox.Show(String.Format("GetDiskSpaceEx: Total Free Bytes in C: is {0}", TotalFreeBytes))

        Dim DriveType As Integer = Win32API.GetDriveType(RootPath)
        Dim DriveTypeName As String = String.Empty

        Select Case DriveType
            Case 2 : DriveTypeName = "Removable"
            Case 3 : DriveTypeName = "Fixed"
            Case 4 : DriveTypeName = "Remote"
            Case 5 : DriveTypeName = "CD-Rom"
            Case 6 : DriveTypeName = "RAM Disk"
            Case Else : DriveTypeName = "Unknown"
        End Select
        MessageBox.Show(String.Format("GetDriveType: {0}", DriveTypeName))
    End Sub
End Class

Partial Public Class Win32API

    Public Declare Function GetDiskFreeSpace Lib "kernel32" _
        Alias "GetDiskFreeSpaceA" (ByVal RootPathName As String,
                                   ByRef SectorsPerCluster As Integer,
                                   ByRef BytesPerSector As Integer,
                                   ByRef NumberOfFreeClusters As Integer,
                                   ByRef TotalNumberOfClusters As Integer) As Integer

    Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" _
        Alias "GetDiskFreeSpaceExA" (ByVal RootPathName As String,
                                     ByRef FreeBytesAvailableToCaller As Integer,
                                     ByRef TotalNumberOfBytes As Integer,
                                     ByRef TotalNumberOfFreeBytes As UInt32) As Integer

    Public Declare Function GetDriveType Lib "kernel32" _
        Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer

End Class

About the Author

Joe Kunk is a Microsoft MVP in Visual Basic, three-time president of the Greater Lansing User Group for .NET, and developer for Dart Container Corporation of Mason, Michigan. He's been developing software for over 30 years and has worked in the education, government, financial and manufacturing industries. Kunk's co-authored the book "Professional DevExpress ASP.NET Controls" (Wrox Programmer to Programmer, 2009). He can be reached via email at

comments powered by Disqus


  • Visual Studio Code Dev Team Cleans Up

    The Visual Studio Code development team focused on some housekeeping in the October update, closing more than 4,000 issues on GitHub, where the cross-platform, open-source editor lives.

  • ML.NET Model Builder Update Boosts Image Classification

    Microsoft announced an update to the Model Builder component of its ML.NET machine learning framework, boosting image classification and adding "try your model" functionality for predictions with sample input.

  • How to Do Naive Bayes with Numeric Data Using C#

    Dr. James McCaffrey of Microsoft Research uses a full code sample and screenshots to demonstrate how to create a naive Bayes classification system when the predictor values are numeric, using the C# language without any special code libraries.

  • Vortex

    Open Source 'Infrastructure-as-Code' SDK Adds .NET Core Support for Working with Azure

    Pulumi, known for its "Infrastructure-as-Code" cloud development tooling, has added support for .NET Core, letting .NET-centric developers use C#, F# and VB.NET to create, deploy, and manage Azure infrastructure.

  • .NET Framework Not Forgotten: Repair Tool Updated

    Even though Microsoft's development focus has shifted to the open-source, cross-platform .NET Core initiative -- with the aging, traditional, Windows-only .NET Framework relegated primarily to fixes and maintenance such as quality and reliability improvements -- the latter is still getting some other attention, as exemplified in a repair tool update.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events