【 解 説 】
ドライブの容量を取得するには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 |
Imports System.Management ' 参照にDLLの追加が必要です Class SurroundingClass Class DriveInfo Public Size As UInt64 Public Used As UInt64 Public Available As UInt64 End Class Public Function getDiskSize(ByVal drive As String) As DriveInfo Dim driveInfo As DriveInfo = New DriveInfo() If Not Directory.Exists(drive & ":\") Then Throw New DirectoryNotFoundException("The drive does not exist") Dim disk As ManagementObject = New ManagementObject("Win32_LogicalDisk.DeviceID=""" & drive & ":""") Dim freespace As UInt64 = CType(disk.Properties("FreeSpace").Value, UInt64) Dim size As UInt64 = CType(disk.Properties("Size").Value, UInt64) driveInfo.size = size driveInfo.used = ((size - freespace)) driveInfo.available = freespace End If Return driveInfo End Function End Class |
■お薦め参考関連資料
Microsoft、Visual Studio、Visual Basic、Visual C#、Windows Vista、Windows Server、SQL Server、Visual C++、Visual Web Developerは、米国 Microsoft Corporationの米国及びその他の国における登録商標または商標です。