ディスクの空き容量を取得(T)         <TOP>


ディスクの空き容量を取得します。(Window2000/WindowXP)

GetLogicalDrives 利用可能ディスクドライブ取得

GetDiskFreeSpace 指定のディレクトリを含むディスクの空き容量を調べる

 

WindowsXPでの例                                               プロパティで確認

 

Windows2000での例                                            プロパティで確認

 

 

'================================================================
'= ディスクの空き容量を取得(Window2000/WindowXP)
'=    (GetDiskFreeSpace.bas)
'================================================================
#include "Windows.bi"

' 利用可能ディスクドライブ取得
Declare Function Api_GetLogicalDrives& Lib "kernel32" Alias "GetLogicalDrives" ()

' 指定のディレクトリを含むディスクの空き容量を調べる
Declare Function Api_GetDiskFreeSpace& Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName$, lpSecPerCluster&, lpBytesPerSector&, lpNumberOfFreeCs&, lpTotalNumberOfClusters&)

Var Shared Combo1 As Object
Var Shared Text(7) As Object
Var Shared Picture1 As Object

Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14
Picture1.Attach GetDlgItem("Picture1")
For i = 0 To 7
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 14
Next

Var Shared Drive As String

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Drives = Api_GetLogicalDrives()        '利用可能なディスクドライブ取得
    If Drives = 0 Then Exit Sub            '関数の失敗

    For i = 0 To 25                        'A〜Zドライブを検索する
        If (Drives And 1) = 1 Then
            Drive = Chr$(65 + i)           'ドライブ名(A〜Z)に変換
            Drive = Drive & ":\"
            Combo1.AddString Drive
        End If
        Drives = Drives \ 2                'ドライブ検索
    Next i
End Sub

'================================================================
'=
'================================================================
Declare Sub Combo1_Change edecl ()
Sub Combo1_Change()
    Var Sector As Long
    Var Bytes As Long
    Var FreeC As Long
    Var TotalC As Long
    Var Ret As Long

    'ドライブ名取得
    Drive = Left$(Combo1.GetText(Combo1.GetCursel), 3)
    Ret = Api_GetDiskFreeSpace(Drive, Sector, Bytes, FreeC, TotalC)

    If RET <> 0 Then
        '総容量 = 総クラスタ数 * クラスタ当たりのセクタ数 * セクタ当たりのバイト数
        Text(4).SetWindowText Format$(TotalC / 1000000 * Sector * Bytes, "###,###.##")

        '使用容量 = 総容量 - 空き容量
        Text(5).SetWindowText Format$((TotalC / 1000000 * Sector * Bytes) - (FreeC / 1000000 * Sector * Bytes), "###,###.##")

        '空き容量 = 空きクラスタ数 * クラスタ当たりのセクタ数 * セクタ当たりのバイト数
        Text(6).SetWindowText Format$(FREEC / 1000000 * Sector * Bytes, "###,###.##")

        '空き容量のパーセンテージ
        Text(7).SetWindowText Format$((FreeC / 1000000 * Sector * Bytes) / (TotalC / 1000000 * Sector * Bytes) * 100, "##.##")

        'グラフ作成
        Picture1.Cls
        Picture1.Connect(0, Picture1.GetHeight - 3) - (Picture1.GetWidth - 3, Picture1.GetHeight - 3) - (Picture1.Getwidth - 3, 0), 15
        Picture1.Line(2, 1) - ((FreeC / 1000000 * Sector * Bytes) / (TotalC / 1000000 * Sector * Bytes) * Picture1.GetWidth , Picture1.GetHeight - 4), , 7, bf
        Picture1.Line((FreeC / 1000000 * Sector * Bytes) / (TotalC / 1000000 * Sector * Bytes) * Picture1.GetWidth + 1, 1) - (Picture1.Getwidth - 4, Picture1.GetHeight - 4), , 3, bf
    Else
        For i = 4 To 7
            Text(i).SetWindowText ""
        Next
        Picture1.Cls
    End If
End Sub

'================================================================
'=
'================================================================
Declare Function GetTotalSpace(Drive As String) As Single
Function GetTotalSpace(Drive As String) As Single
    Var SectorPerCluster As Long
    Var BytePerCluster As Long
    Var FreeC As Long
    Var TotalC As Long

    If Api_GetDiskFreeSpace(Drive, SectorPerCluster, BytePerCluster, FreeC, TotalC) = 0 Then
        GetTotalSpace = 0
    Else
        GetTotalSpace = ((TotalC / 1000000) * SectorPerCluster * BytePerCluster)
    End If
End Function

'================================================================
'=
'================================================================
Declare Function GetFreeSpace(Drive As String) As Single
Function GetFreeSpace(Drive As String) As Single
    Var SectorPerCluster As Long
    Var BytePerCluster As Long
    Var FreeC As Long
    Var TotalC As Long

    If Api_GetDiskFreeSpace(Drive, SectorPerCluster, BytePerCluster, FreeC, TotalC) = 0 Then
        GetFreeSpace = 0
    Else
        GetFreeSpace = ((FreeC / 1000000) * SectorPerCluster * BytePerCluster)
    End If
End Function

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End