【 解 説 】
ドライブの容量を取得するにはManagementObjectクラスを使用します。
以下のコードはドライブのサイズ、使用済みサイズ、未使用サイズをDriveInfoというクラスに格納して返します。
【 例 文 】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System.Management; // 参照にDLLの追加が必要です。 class DriveInfo { public UInt64 Size; public UInt64 Used; public UInt64 Available; } public DriveInfo getDiskSize(String drive) { DriveInfo driveInfo = new DriveInfo(); if(!Directory.Exists(drive+@":\")) { throw new DirectoryNotFoundException("The drive does not exist"); ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\""); UInt64 freespace = (UInt64)disk.Properties["FreeSpace"].Value ; UInt64 size = (UInt64)disk.Properties["Size"].Value ; driveInfo.size = size; driveInfo.used = ((size - freespace)); driveInfo.available = freespace; } return driveInfo; } |
■お薦め参考関連資料
Microsoft、Visual Studio、Visual Basic、Visual C#、Windows Vista、Windows Server、SQL Server、Visual C++、Visual Web Developerは、米国 Microsoft Corporationの米国及びその他の国における登録商標または商標です。