[BlueLeaf1336]> PROGRAM> Natural Born Junk>

ドライブ情報取得

historyTOP

2002/10/27:作成

memoTOP

利用可能なドライブ文字列の列挙
ドライブの準備完了チェック
ディスクドライブの種類
指定ディスクの総容量(バイト)取得
指定ディスクの空き容量(バイト)取得

codeTOP

(*
    =================================================
    利用可能なドライブ文字列の列挙
    =================================================
*)
function AvailableDrives: string;
var
    DriveNum: Integer;
    DriveChar: Char;
    DriveBits: set of 0..25;
begin
    Result := '';
    Integer(DriveBits) := GetLogicalDrives;
    for DriveNum := 0 to 25 do
    begin
        if not (DriveNum in DriveBits) then Continue;
        DriveChar := Upcase(Char(DriveNum + Ord('a')));
        Result := Result + DriveChar;
    end;
end;

(*
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    テスト
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
*)
procedure TForm1.Button4Click(Sender: TObject);
begin
    ShowResult('利用可能なドライブ:' + AvailableDrives);
end;

(*
    =================================================
    ドライブの準備完了チェック
    =================================================
*)
function DriveCheck(Drive: Char; var Err: string): Boolean;
var
    OldMode: DWORD;
    DriveType: Byte;
begin
    Err     := '';
    Result  := False;
    OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);

    try
        DriveType := Windows.GetDriveType(PChar(Drive + ':\'));
        case DriveType of
        0 : Err := 'not determined';
        1 : Err := 'not exists';
        DRIVE_REMOVABLE, DRIVE_CDROM:
            begin
                Result := DirectoryExists(Drive + ':\');
                if GetLastError = ERROR_NOT_READY then
                begin
                    Err := 'メディアが装着されていません。';
                end else
                if GetLastError = ERROR_GEN_FAILURE then
                begin
                    Err := 'メディアがフォーマットされていません。';
                end else
                begin
                    Err := '状態が取得できません。';
                end;
            end;
        else
            Result := True;
        end;
    finally
        SetErrorMode(OldMode);
    end;
end;

(*
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    テスト
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
*)
procedure TForm1.Button3Click(Sender: TObject);
var
    Drive: Char;
    Err  : string;
begin
    for Drive := 'A' to 'Z' do
    begin
        if DriveCheck(Drive, Err) then
        begin
            ShowResult(Drive + ':\    ' + 'OK');
        end else
        begin
            ShowResult(Drive + ':\    ' + Err);
        end;
    end;
end;

(*
    =================================================
    ディスクドライブの種類を取得する
    =================================================
*)
function GetDriveType(Drive: Char): string;
var
    DriveType: Byte;
begin
    DriveType := Windows.GetDriveType(PChar(Drive + ':\'));
    case DriveType of
        0               :   Result := 'not determined';
        1                :   Result := 'not exists';
        DRIVE_REMOVABLE    :   Result := 'removable drive';
        DRIVE_FIXED        :   Result := 'fixed drive';
        DRIVE_REMOTE    :   Result := 'remote (network) drive';
        DRIVE_CDROM        :   Result := 'cd-rom drive';
        DRIVE_RAMDISK    :   Result := 'ram disk';
    end;
end;

(*
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    テスト
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
*)
procedure TForm1.Button2Click(Sender: TObject);
var
    Drive: Char;
begin
    for Drive := 'A' to 'Z' do
    begin
        ShowResult(Drive + ':\    ' + GetDriveType(Drive));
    end;
end;

(*
    =================================================
    ドライブ文字列をDiskFree/DiskSizeで使用できる数値表示に変換
    =================================================
*)
function DriveCharToByte(Drive: Char): Byte;
const
    DRIVES = '#ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
    //カレントドライブ=0、A=1、B=2、C=3、・・・だそうです。
    Result := Pos(UpCase(Drive), DRIVES) - 1
end;

(*
    =======================================================
    指定ディスクの総容量(バイト)取得
    =======================================================
*)
function GetDiskSize(Drive: Char): Int64;
begin
    Result := DiskSize(DriveCharToByte(Drive));
end;

(*
    =======================================================
    指定ディスクの空き容量(バイト)取得
    =======================================================
*)
function GetDiskFree(Drive: Char): Int64;
begin
    Result := DiskFree(DriveCharToByte(Drive));
end;

(*
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    テスト
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++
*)
procedure TForm1.Button1Click(Sender: TObject);
var
    Drive: Char;
begin
    for Drive := 'A' to 'Z' do
    begin
        ShowResult(Format('%s: Total %d bytes', [Drive, GetDiskSize(Drive)]));
        ShowResult(Drive + ': Free   ' + IntToStr(GetDiskFree(Drive)) + 'bytes');
    end;
end;

EOFTOP