Share via


_getdiskfree

取得磁片磁碟機的相關資訊,例如叢集總計、可用的叢集、每個叢集的磁區,以及每個磁區的位元組。

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

unsigned _getdiskfree(
   unsigned drive,
   struct _diskfree_t * driveinfo
);

參數

drive
您想要的磁碟機資訊。

driveinfo
_diskfree_t 結構,其中將填入該磁碟機的相關資訊。

傳回值

如果此函式成功,則傳回值為零。 如果此函式失敗,則傳回值是錯誤碼。 會針對此作業系統所傳回的任何錯誤而設定 errno 值。 如需 所指示 errno 之錯誤狀況的詳細資訊,請參閱 errno 常數

備註

_diskfree_t 結構在 Direct.h 中定義。

struct _diskfree_t {
   unsigned total_clusters;      // The total number of clusters, both used and available, on the disk.
   unsigned avail_clusters;      // The number of unused clusters on the disk.
   unsigned sectors_per_cluster; // The number of sectors in each cluster.
   unsigned bytes_per_sector;    // The size of each sector in bytes.
};

這個函式會驗證它的參數。 如果指標是或指定不正確 driveinfo 磁片磁碟機,此函式會叫用不正確參數處理常式,如參數驗證 中所述 driveNULL 如果允許繼續執行,函式會傳回 EINVAL,並將 errno 設為 EINVAL。 有效的磁碟機範圍為 0 到 26。 為 0 的 drive 值會指定目前的磁碟機,之後數字會對應至英文字母,使得 1 表示磁碟機 A,3 表示磁碟機 C ,依此類推。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
_getdiskfree <direct.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_getdiskfree.c
// compile with: /c
#include <windows.h>
#include <direct.h>
#include <stdio.h>

int main(int argc, char* argv[]) 
{
    ULONG uDriveMask = _getdrives();

    for (unsigned uDrive = 1; uDrive <= 26; ++uDrive) 
    {
        if (uDriveMask & 1)
        {
            struct _diskfree_t df = { 0 };
            unsigned uErr = _getdiskfree(uDrive, &df);
            printf("\nDrive: %c\n", uDrive + 'A' - 1);
            
            if (uErr == 0)
            {
                printf("\tTotal clusters:      %11u\n", df.total_clusters);
                printf("\tAvailable clusters:  %11u\n", df.avail_clusters);
                printf("\tSectors per cluster: %11u\n", df.sectors_per_cluster);
                printf("\tBytes per sector:    %11u\n", df.bytes_per_sector);
            }
            else
            {
                WCHAR errMsg[80];
                unsigned uLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
                    uErr, 0, errMsg, sizeof(errMsg), NULL);
                printf("%S\n", errMsg);
            }
        }
        uDriveMask >>= 1;
    }
}
Drive: C
        Total clusters:        249754111
        Available clusters:    160184686
        Sectors per cluster:           8
        Bytes per sector:            512

另請參閱

目錄控制項