可能重複:
Get drive information (free space, etc.) for drives on Windows and populate a memo box德爾福 - 枚舉的盤(S)和其他驅動器的Windows PC上
我很新的編程(尤其是德爾福),並一直未能查找有關如何枚舉PC上所有驅動器的任何示例。
我真的很在乎硬盤和光驅,但我一直無法找到任何可用的東西。
任何人都可以指向一個良好的工作樣本的方向嗎?
可能重複:
Get drive information (free space, etc.) for drives on Windows and populate a memo box德爾福 - 枚舉的盤(S)和其他驅動器的Windows PC上
我很新的編程(尤其是德爾福),並一直未能查找有關如何枚舉PC上所有驅動器的任何示例。
我真的很在乎硬盤和光驅,但我一直無法找到任何可用的東西。
任何人都可以指向一個良好的工作樣本的方向嗎?
最簡單的方法實際上是使用sysutils.pas
文件中的GetDiskFreeSpaceEx
。
這個例子有2個部分。第一是使用GetDiskFreeSpaceEX
的重要部分。
function DriveSpace(DriveLetter : String; var FreeSpace, UsedSpace, TotalSpace : int64) : Boolean;
begin
Result := SysUtils.GetDiskFreeSpaceEx(Pchar(DriveLetter), UsedSpace, TotalSpace, @FreeSpace);
if UsedSpace > 0 then
UsedSpace := TotalSpace - FreeSpace;
if not Result then
begin
UsedSpace := 0;
TotalSpace := 0;
FreeSpace := 0;
end;
end;
如果你要要求你已經知道的驅動器盤符驅動器,如C:那麼這就是你所需要的。
用法是這樣的:
var
FS,
US,
TS : Int64
begin
DriveSpace('C:', FS, US, TS);
//Do something with the 3 variables.
end;
說了這麼多,如果你想找到的驅動器,以及可以使用這樣的事情:
procedure ListDrivesOfType(DriveType : Integer; var Drives : TStringList);
var
DriveMap,
dMask : DWORD;
dRoot : String;
I : Integer;
begin
dRoot := 'A:\'; //' // work around highlighting
DriveMap := GetLogicalDrives;
dMask := 1;
for I := 0 to 32 do
begin
if (dMask and DriveMap) <> 0 then
if GetDriveType(PChar(dRoot)) = DriveType then
begin
Drives.Add(dRoot[1] + ':');
end;
dMask := dMask shl 1;
Inc(dRoot[1]);
end;
end;
注意的DriveType整數,應該是以下之一:
DRIVE_UNKNOWN = 0;
DRIVE_NO_ROOT_DIR = 1;
DRIVE_REMOVABLE = 2;
DRIVE_FIXED = 3;
DRIVE_REMOTE = 4;
DRIVE_CDROM = 5;
DRIVE_RAMDISK = 6;
(我已將這些直接從windows.pas
)
現在終於回答你的問題(這是很粗糙)以下將爲所有FIXED HARD DRIVES
信息添加到備忘錄(被稱爲memo1):
Procedure TAform.SomeNameICantThinkOfNow;
const
BytesPerMB = 1048576;
var
MyDrives : TStringlist;
I : Integer;
FreeSpace,
UsedSpace,
TotalSpace : int64;
begin
MyDrives := TStringlist.Create;
ListDrivesOfType(DRIVE_FIXED, MyDrives);
Memo1.Lines.Clear;
for I := 0 to MyDrives.Count - 1 do
begin
FreeSpace := 0;
UsedSpace := 0;
TotalSpace := 0;
if DriveSpace(MyDrives.Strings[I], FreeSpace, UsedSpace, TotalSpace) then
begin
FreeSpace := FreeSpace div BytesPerMB;
UsedSpace := UsedSpace div BytesPerMB;
TotalSpace := TotalSpace div BytesPerMB;
Memo1.Lines.Add('Drive: ' + MyDrives.Strings[I] + ' = Free Space :' + IntToStr(FreeSpace) +
' Used Space: ' + IntToStr(UsedSpace) + ' Total Space: ' + IntToStr(TotalSpace));
end;
end;
end;
我沒有說出來會討厭!我剛剛在IDE中運行它,它的工作原理,我已經做了MB,但你真的應該轉換爲雙精度,並選擇你的格式,如果作爲MB更精確,因爲我上面創建的例子當然會收起來。
希望這是一些小的幫助。
謝謝我現在看看這個,我會給你一個投票,但看起來我對這個網站來說太新了。對不起 – DelphiDelver 2011-04-12 13:04:27
我真的很抱歉,我在發佈之前搜索過,但沒有看到其他帖子。我應該刪除我的問題嗎? – DelphiDelver 2011-04-12 13:45:17
沒關係。它*不容易搜索重複。我想你可以在這裏留下你的問題(除此之外,我不認爲你已經發布了回答,所以你甚至不能刪除它)。另外,這種方式在將來會更容易在SO上找到這個話題,因爲那時有兩個問題略有不同的措辭。 – 2011-04-12 13:49:41
你不應該擔心刪除它,並且在一個非常自私的紙條上,我很高興,因爲你接受了答案,原來的海報沒有。這就是生活。 – Reallyethical 2011-04-12 13:50:21