.NET Framework 2.0からは、DriveInfoクラスによりドライブの情報を取得できます。ドライブのサイズはTotalSize、使用できる空き容量はAvailableFreeSpace、空き容量の合計はTotalFreeSpaceプロパティで取得できます。AvailableFreeSpaceは現在のユーザーが利用できる空き容量で、TotalFreeSpaceはそれ以外も含みます。
'C:ドライブの情報を取得する Dim drive As New System.IO.DriveInfo("C") 'ドライブの準備ができているか調べる If drive.IsReady Then Console.WriteLine("呼び出し側が利用できるバイト数:{0}", _ drive.AvailableFreeSpace) Console.WriteLine("ドライブ全体のバイト数:{0}", _ drive.TotalSize) Console.WriteLine("ドライブ全体の空きバイト数:{0}", _ drive.TotalFreeSpace) End If
//C:ドライブの情報を取得する System.IO.DriveInfo drive = new System.IO.DriveInfo("C"); //ドライブの準備ができているか調べる if (drive.IsReady) { Console.WriteLine("呼び出し側が利用できるバイト数:{0}", drive.AvailableFreeSpace); Console.WriteLine("ドライブ全体のバイト数:{0}", drive.TotalSize); Console.WriteLine("ドライブ全体の空きバイト数:{0}", drive.TotalFreeSpace); }
次にWMI使って取得する方法を紹介します。WMIはWindows2000以上のOSで使用できます。(それ以外のOSでもインストールすれば使用できます。こちらからダウンロードできるようです。)
下の例ではSystem.Management.dllを参照設定に追加する必要があります。
Dim mo As New System.Management.ManagementObject("Win32_LogicalDisk=""C:""") 'C:のドライブの容量を取得する Dim ts As UInt64 = CType(mo.Properties("Size").Value, UInt64) Console.WriteLine("C:ドライブのサイズは{0}バイトです。", ts) 'C:のドライブの空き容量を取得する Dim fs As UInt64 = CType(mo.Properties("FreeSpace").Value, UInt64) Console.WriteLine("C:ドライブの空き容量は{0}バイトです。", fs) mo.Dispose()
System.Management.ManagementObject mo =
new System.Management.ManagementObject("Win32_LogicalDisk=\"C:\"");
//C:のドライブの容量を取得する
ulong ts = (ulong)mo.Properties["Size"].Value;
Console.WriteLine("C:ドライブのサイズは{0}バイトです。", ts);
//C:のドライブの空き容量を取得する
ulong fs = (ulong)mo.Properties["FreeSpace"].Value;
Console.WriteLine("C:ドライブの空き容量は{0}バイトです。", fs);
mo.Dispose();
Win32 APIのGetDiskFreeSpaceEx関数を使って取得することもできます。以下にその例を示します。
<System.Runtime.InteropServices.DllImport("kernel32.dll", _
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Public Shared Function GetDiskFreeSpaceEx( _
ByVal lpDirectoryName As String, _
ByRef lpFreeBytesAvailable As UInt64, _
ByRef lpTotalNumberOfBytes As UInt64, _
ByRef lpTotalNumberOfFreeBytes As UInt64) As _
<System.Runtime.InteropServices.MarshalAs( _
System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
End Function
'Button1のClickイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
'呼び出し側が利用できるバイト数
Dim freeBytesAvailable As UInt64
'ディスク全体のバイト数
Dim totalNumberOfBytes As UInt64
'ディスク全体の空きバイト数
Dim totalNumberOfFreeBytes As UInt64
'ディスク"C"の空き容量を取得する
GetDiskFreeSpaceEx("C:\", freeBytesAvailable, _
totalNumberOfBytes, totalNumberOfFreeBytes)
Console.WriteLine("呼び出し側が利用できるバイト数:{0}", _
freeBytesAvailable)
Console.WriteLine("ディスク全体のバイト数:{0}", _
totalNumberOfBytes)
Console.WriteLine("ディスク全体の空きバイト数:{0}", _
totalNumberOfFreeBytes)
End Sub
[System.Runtime.InteropServices.DllImport("kernel32.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[return: System.Runtime.InteropServices.MarshalAs(
System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(
string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
//Button1のClickイベントハンドラ
private void Button1_Click(object sender, EventArgs e)
{
//呼び出し側が利用できるバイト数
ulong freeBytesAvailable;
//ディスク全体のバイト数
ulong totalNumberOfBytes;
//ディスク全体の空きバイト数
ulong totalNumberOfFreeBytes;
//ディスク"C"の空き容量を取得する
GetDiskFreeSpaceEx("C:\\", out freeBytesAvailable,
out totalNumberOfBytes, out totalNumberOfFreeBytes);
Console.WriteLine("呼び出し側が利用できるバイト数:{0}",
freeBytesAvailable);
Console.WriteLine("ディスク全体のバイト数:{0}",
totalNumberOfBytes);
Console.WriteLine("ディスク全体の空きバイト数:{0}",
totalNumberOfFreeBytes);
}
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。