2

I am trying to get the cluster size of a disk in C#. Everything I have found says to use "GetFreeDiskSpace," but I can't get it to work. It appears as if I am missing a using or something.

When I Google the The name 'GetDiskFreeSpace' does not exist in the current context it brings up everything except for this specific error. If I do an exact phrase search, Google says nothing is found and then displays the non-exact phrase search results.

I am trying to determine where the GetFreeDiskSpace comes from, not how to fix the The name 'UnknownKeyWord' does not exist in the current context message.

I need to get the actual cluster size of a disk, not so I can determine the size on disk, but so I can populate a ComboBox.

NOTE: I am using VS 2010.

Here are the usings I have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Globalization;
using System.Management;
using System.Runtime.InteropServices;

I also have the following:

// Pinvoke for API function
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]

The code (which is not finished...I need to parse out the information from GetFreeDiskSpace) I have to get the cluster size is:

private void btnRefreshDrives_Click(object sender, EventArgs e)
{
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        if (d.IsReady)
        {                    
            strDriveInfo = d.Name + " " + d.VolumeLabel;
            strCurrentFS = d.DriveFormat;
            strDriveLetter = d.Name;
            // The GetFreeDiskSpace has the red squiggly line under it in VS.
            ClusterSize = SectorsPerCluster * BytesPerSector;
            GetDiskFreeSpace(strDriveLetter, out SectorsPerCluster, out BytesPerSector, out NumberOfFreeClusters, out TotalNumberOfClusters);
        }
    }
}
kennyzx
  • 12,845
  • 6
  • 39
  • 83
Tornado726
  • 352
  • 1
  • 7
  • 16
  • http://stackoverflow.com/questions/14465187/get-available-disk-free-space-for-a-given-path-on-windows – Mitch Wheat Dec 01 '14 at 01:16
  • 2
    you did not write down the method `[DllImport]` decorates, _where_ is the definition for `GetFreeDiskSpace`? – kennyzx Dec 01 '14 at 01:27
  • "I am trying to determine where the 'GetFreeDiskSpace' comes from." It doesn't come from anywhere. You have to make it. – Raymond Chen Dec 01 '14 at 01:34

2 Answers2

3

If you want to use GetFreeDiskSpace, you need to import the function definition:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool GetDiskFreeSpace(string lpRootPathName, 
   out ulong lpSectorsPerCluster, 
   out ulong lpBytesPerSector, 
   out ulong lpNumberOfFreeClusters, 
   out ulong lpTotalNumberOfClusters);
Donal
  • 31,121
  • 10
  • 63
  • 72
  • 1
    Just an FYI if you happen across this. You should cast these uint outputs to ulong for any calculations (e.g. if determining free space) as most modern drives will overflow a uint. – Kris Oye Feb 19 '21 at 06:53
0

There are two problems:

  1. You have not actually declared (at least what is copied into the code) the GetDiskFreeSpace method. It should be:

    [DllImport("kernel32",SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetDiskFreeSpace(string lpRootPathName, out int lpSectorsPerCluster, out int lpBytesPerSector, out int lpNumberOfFreeClusters, out int lpTotalNumberOfClusters);
    
  2. You are calculating the ClusterSize BEFORE retrieving the values. It should be after:

    GetDiskFreeSpace(strDriveLetter, out SectorsPerCluster, out BytesPerSector, out NumberOfFreeClusters, out TotalNumberOfClusters);
    var ClusterSize = SectorsPerCluster * BytesPerSector;
    

I have verified that with these two changes (and appropriate error checking) this works correctly.

competent_tech
  • 44,465
  • 11
  • 90
  • 113