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; } [DllImport("coredll")] 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
Public
Dim result As New DiskFreeSpace
If GetDiskFreeSpaceEx(directoryName, result.FreeBytesAvailable, result.TotalBytes, result.TotalFreeBytes) = False Then
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.