Home
Manage Your Code
Snippet: Get Disk info, e.g. free space (C#)
Title: Get Disk info, e.g. free space Language: C#
Description: using GetDiskFreeSpaceEx in kernel32.dll Views: 156
Author: Yan Xing Yang Date Added: 7/31/2008
Copy Code  
1public sealed class DriveInfo
2{
3    [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")]
4    private static extern long GetDiskFreeSpaceEx(string lpDirectoryName,
5        out long lpFreeBytesAvailableToCaller,
6        out long lpTotalNumberOfBytes,
7        out long lpTotalNumberOfFreeBytes);
8
9    public static long GetInfo(string drive, out long available, out long total, out long free)
10    {
11        return GetDiskFreeSpaceEx(drive, out available, out total, out free);
12    }
13
14    public static DriveInfoSystem GetInfo(string drive)
15    {
16        long result, available, total, free;
17        result = GetDiskFreeSpaceEx(drive, out available, out total, out free);
18        return new DriveInfoSystem(drive, result, available, total, free);
19    }
20}
21
22public struct DriveInfoSystem
23{
24    public readonly string Drive;
25    public readonly long Result;
26    public readonly long Available;
27    public readonly long Total;
28    public readonly long Free;
29
30    public DriveInfoSystem(string drive, long result, long available, long total, long free)
31    {
32        this.Drive = drive;
33        this.Result = result;
34        this.Available = available;
35        this.Total = total;
36        this.Free = free;
37    }
38}
39
40
Usage
DriveInfoSystem info = DriveInfo.GetInfo("c:");