Free space on Storage Cards

You can determine the total size of a Storage Card and available free bytes using the GetDiskFreeSpaceEx API function. Below is a “mini-wrapper” around the function which returns a structure with the three return values. You’ll notice in this example I’m marshalling longs (64bit Integers), values greater than 32bit cannot be marshalled by value but can be marshalled by reference as in this case. The DiskFreeSpace struct contains three long members to hold the accessible free bytes, total bytes and total free bytes.

public static DiskFreeSpace GetDiskFreeSpace(string directoryName)
DiskFreeSpace result = new DiskFreeSpace();

if(!GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable,

        ref result.TotalBytes, ref result.TotalFreeBytes))
throw new Win32Exception(Marshal.GetLastWin32Error(), “Error retrieving free disk space”);
return result;

public struct DiskFreeSpace
public long FreeBytesAvailable;

public long TotalBytes;

public long TotalFreeBytes;

private static extern bool GetDiskFreeSpaceEx(string directoryName,
ref long freeBytesAvailable,
ref long totalBytes,
ref long totalFreeBytes);


Or if you prefer Visual Basic:-

Public Function GetDiskFreeSpace(ByRef directoryName As String) As DiskFreeSpace

    Dim result As New DiskFreeSpace

    If GetDiskFreeSpaceEx(directoryName, result.FreeBytesAvailable, result.TotalBytes, result.TotalFreeBytes) = False Then

        Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), “Error retrieving free disk space”)

    End If

    Return result

End Function

Public Structure DiskFreeSpace

    Public FreeBytesAvailable As Long

    Public TotalBytes As Long

    Public TotalFreeBytes As Long

End Structure

Private Declare Function GetDiskFreeSpaceEx Lib “coredll.dll” (ByVal directoryName As String, ByRef freeBytesAvailable As Long, ByRef totalBytes As Long, ByRef totalFreeBytes As Long)