선언:

C#

[DllImport("kernel32")]
public static extern Boolean GetDiskFreeSpace(String lpRootPathName, out UInt32 lpSectorsPerCluster, out UInt32 lpBytesPerSector, out UInt32 lpNumberOfFreeClusters, out UInt32 lpTotalNumberOfClusters);


VB.NET

<DllImport("kernel32")> _
Public Shared Function GetDiskFreeSpace(lpRootPathName As [String], ByRef lpSectorsPerCluster As UInt32, ByRef lpBytesPerSector As UInt32, ByRef lpNumberOfFreeClusters As UInt32, ByRef lpTotalNumberOfClusters As UInt32) As [Boolean]
End Function




사용 예제:

C#

using System;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        [DllImport("kernel32")]
        public static extern Boolean GetDiskFreeSpace(String lpRootPathName, out UInt32 lpSectorsPerCluster, out UInt32 lpBytesPerSector, out UInt32 lpNumberOfFreeClusters, out UInt32 lpTotalNumberOfClusters);
        
        public static void Main(string[] args) {
            UInt32 spc, bps, nfc, tnc;
            GetDiskFreeSpace(@"C:\", out spc, out bps, out nfc, out tnc);
            Console.WriteLine("Sectors per Cluster    : {0:N0}", spc);
            Console.WriteLine("Bytes per Sector       : {0:N0}", bps);
            Console.WriteLine("Number of Free Clusters: {0:N0}", nfc);
            Console.WriteLine("Total Clusters         : {0:N0}", tnc);
            
            // 계산.
            // A 섹터 용량: bps (섹터 당 바이트 수)
            // B 클러스터 용량: spc * A (클러스터 당 섹터 수 * 섹터 당 바이트 수)
            // C 디스크의 전체 용량 구하는 법: tnc * B (전체 클러스터 * 클러스터 용량)
            // D 사용 가능한 용량 구하는 법: nfc * B (사용 가능한 클러스터 * 클러스터 용량)
            // E 사용 중인 용량 구하는 법: C - D
            UInt64 clusterSize = spc * bps;
            UInt64 totalSize = tnc * clusterSize;
            UInt64 freeSize = nfc * clusterSize;
            UInt64 usedSize = totalSize - freeSize;
            
                            
            // 한번 1024로 나누면 KB
            // 두번 나누면 MB
            // 세번 나누면 GB
            Console.WriteLine("디스크 전체 용량: {0:N2} GB ({1:N0} Bytes)", totalSize / 1024 / 1024 / 1024, totalSize);
            Console.WriteLine("사용 중인 용량: {0:N2} GB ({1:N0} Bytes)", usedSize / 1024 / 1024 / 1024, usedSize);
            Console.WriteLine("사용 가능한 용량: {0:N2} GB ({1:N0} Bytes)", freeSize / 1024 / 1024 / 1024, freeSize);

            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System.Runtime.InteropServices

Namespace ApiReference
    Class ApiExample
        <DllImport("kernel32")> _
        Public Shared Function GetDiskFreeSpace(lpRootPathName As [String], ByRef lpSectorsPerCluster As UInt32, ByRef lpBytesPerSector As UInt32, ByRef lpNumberOfFreeClusters As UInt32, ByRef lpTotalNumberOfClusters As UInt32) As [Boolean]
        End Function

        Public Shared Sub Main(args As String())
            Dim spc As UInt32, bps As UInt32, nfc As UInt32, tnc As UInt32
            GetDiskFreeSpace("C:\", spc, bps, nfc, tnc)
            Console.WriteLine("Sectors per Cluster    : {0:N0}", spc)
            Console.WriteLine("Bytes per Sector       : {0:N0}", bps)
            Console.WriteLine("Number of Free Clusters: {0:N0}", nfc)
            Console.WriteLine("Total Clusters         : {0:N0}", tnc)

            ' 계산.
            ' A 섹터 용량: bps (섹터 당 바이트 수)
            ' B 클러스터 용량: spc * A (클러스터 당 섹터 수 * 섹터 당 바이트 수)
            ' C 디스크의 전체 용량 구하는 법: tnc * B (전체 클러스터 * 클러스터 용량)
            ' D 사용 가능한 용량 구하는 법: nfc * B (사용 가능한 클러스터 * 클러스터 용량)
            ' E 사용 중인 용량 구하는 법: C - D
            Dim clusterSize As UInt64 = spc * bps
            Dim totalSize As UInt64 = tnc * clusterSize
            Dim freeSize As UInt64 = nfc * clusterSize
            Dim usedSize As UInt64 = totalSize - freeSize


            ' 한번 1024로 나누면 KB
            ' 두번 나누면 MB
            ' 세번 나누면 GB
            Console.WriteLine("디스크 전체 용량: {0:N2} GB ({1:N0} Bytes)", totalSize \ 1024 \ 1024 \ 1024, totalSize)
            Console.WriteLine("사용 중인 용량: {0:N2} GB ({1:N0} Bytes)", usedSize \ 1024 \ 1024 \ 1024, usedSize)
            Console.WriteLine("사용 가능한 용량: {0:N2} GB ({1:N0} Bytes)", freeSize \ 1024 \ 1024 \ 1024, freeSize)

            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

lpRootPathName - 정보를 가져올 디스크의 루트 디렉터리를 입력합니다.

lpSectorsPerCluster - 클러스터 당 섹터 수가 저장될 변수를 입력합니다.

lpBytesPerSector - 섹터 당 바이트 수가 저장될 변수를 입력합니다.

lpNumberOfFreeClusters - 사용 가능한 클러스터의 수가 저장될 변수를 입력합니다.

lpTotalNumberOfClusters - 모든 클러스터의 수가 저장될 변수를 입력합니다.




API 설명:

사용 가능한 용량 정보를 포함한 디스크의 정보를 가져옵니다.




참고:

GetDiskFreeSpace (MSDN)




요구 사항:

클라이언트: Windows XP+

서버: Windows Server 2003+




비고:

GetDiskFreeSpaceEx API를 이용하면 클러스터, 섹터를 가지고 바이트 수를 계산하는 것을 안할 수 있습니다.

(GetDiskFreeSpaceEx API는 바로 사용 가능한 바이트 수와 전체 바이트 수를 가져오기 때문)

'API Reference' 카테고리의 다른 글

50. PROCESS_MEMORY_COUNTERS_EX  (0) 2014.12.12
49. PROCESS_MEMORY_COUNTERS  (0) 2014.12.11
47. EmptyWorkingSet  (1) 2014.11.12
46. GetFileSize  (0) 2014.11.09
45. OpenFile  (0) 2014.11.09

+ Recent posts