using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Uchukamen.WZero3
{
public class DiskStatus
{
[DllImport("coredll.dll")]
public static extern bool GetDiskFreeSpaceEx
(
string directory,
ref UInt64 lpFreeBytesAvailableToCaller,
ref UInt64 lpTotalNumberOfBytes,
ref UInt64 lpTotalNumberOfFreeBytes
);
#region プロパティ
/// <summary>
/// The total number of free bytes on the disk
/// that are available to the user associated with the calling thread.
/// </summary>
private UInt64 freeBytesAvailableToCaller = 0;
public UInt64 FreeBytesAvailableToCaller
{
get { return freeBytesAvailableToCaller; }
}
public float FreeMB
{
get { return (float)freeBytesAvailableToCaller / 1024F / 1024F; }
}
/// <summary>
/// The total number of bytes on the disk
/// that are available to the user associated with the calling thread.
/// </summary>
private UInt64 totalNumberOfBytes = 0;
public UInt64 TotalNumberOfBytes
{
get { return totalNumberOfBytes; }
}
public float TotalMB
{
get { return (float)totalNumberOfBytes/1024F/1024F; }
}
/// <summary>
/// The total number of free bytes on the disk.
/// </summary>
private UInt64 totalNumberOfFreeBytes = 0;
public UInt64 TotalNumberOfFreeBytes
{
get { return totalNumberOfFreeBytes; }
}
#endregion
/// <summary>
/// ディスク容量を取得する。
/// </summary>
/// <param name="path">ディスクのパス</param>
public void Get(string path)
{
bool res = GetDiskFreeSpaceEx(
path,
ref freeBytesAvailableToCaller,
ref totalNumberOfBytes,
ref totalNumberOfFreeBytes);
}
}
}
呼び出し
DiskStatus dstat = new DiskStatus();
dstat.Get("\\");
textBox1.Text +=
"空き容量 " + dstat.FreeMB.ToString("n2") + " MB\r\n" +
"合計容量 " + dstat.TotalMB.ToString("n2") + " MB\r\n";