Get cluster size on disk in C # - Get error in 'GetDiskFreeSpace'

I am trying to get disk cluster size in C #. All I found is talking about using "GetFreeDiskSpace", but I can't get it to work. Looks like I'm missing out on usage or something like that.

When I Google The name 'GetDiskFreeSpace' does not exist in the current context , it causes everything except this specific error. If I do an exact search for a phrase, Google says that nothing was found, and then displays the results of an inaccurate phrase search.

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

I need to get the actual cluster size on the disk, but I can not determine the size on the disk, but I can fill the ComboBox.

NOTE. I am using VS 2010.

Here is what 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)] 

Code (which is not finished ... I need to parse the information from GetFreeDiskSpace). I need to get the cluster size:

 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); } } } 
+2
c # pinvoke
source share
2 answers

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 uint lpSectorsPerCluster, out uint lpBytesPerSector, out uint lpNumberOfFreeClusters, out uint lpTotalNumberOfClusters); 
+3
source share

There are two problems:

  • 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); 
  • You compute ClusterSize before retrieving the values. This should be after:

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

I checked that with these two changes (and corresponding error checking) this works correctly.

0
source share

All Articles