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

爷,独闯天下 提交于 2020-01-04 13:39:04

问题


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);
        }
    }
}

回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/27220437/get-the-cluster-size-of-a-disk-in-c-get-error-on-getdiskfreespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!