Delphi-枚举Windows PC上的磁盘和其他驱动器 [英] Delphi - Enumerate the Disk(s) and other Drives on Windows PC

查看:135
本文介绍了Delphi-枚举Windows PC上的磁盘和其他驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我对编程非常陌生(尤其是Delphi),并且无法找到有关如何进行编程的任何示例。枚举PC上的所有驱动器。

I am quite new to programing (especially Delphi), and have been unable to find any examples as to how to enumerate all the drives on a PC.

我真的很在乎硬盘和CD-Rom驱动器,但是我找不到任何可用的东西。

I really care about Hard Disks and CD-Rom drives but I have been unable to find anything useable.

有人可以指出我的工作样本吗?

Can anyone point me in the direction of a good working sample?

推荐答案

最简单的方法实际上是从 sysutils.pas 文件使用 GetDiskFreeSpaceEx

The simplest way is actually to use GetDiskFreeSpaceEx from the sysutils.pas file.

此示例分为两部分。第一个是使用 GetDiskFreeSpaceEX 的重要部分。

There are 2 parts to this example. the 1st is the important part using 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:是你所需要的全部。

If you are going to request drives that you already know the drive letter for such as C: then that is all you need.

用法类似于:

var
  FS,
  US,
  TS : Int64
begin
  DriveSpace('C:', FS, US, TS);
  //Do something with the 3 variables.
end;

前面已经说过,如果您也想找到驱动器,则可以使用以下命令:

Having said that if you want to find the drives as well you could use something like this:

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整数应为以下之一:

Note the DriveType integer, should be one of the following:

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

现在终于可以回答您的问题了(这很粗糙),以下内容将信息添加到所有固定硬盘驱动器的备忘录(称为memo1)

Now finally to answer your question (and this is very rough) the following would add information into a memo (called memo1) for all FIXED HARD DRIVES:

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进行工作,则应该转换为Double并选择格式,因为我在上面创建的示例更加精确,当然会四舍五入。

I did say it would be nasty! I have just run this up in the IDE and it works, I have done as MB but really you should convert to Double and choose your formatting if doing as MB to be more precise as the example I have create above will of course just round up.

希望这对您有所帮助。

这篇关于Delphi-枚举Windows PC上的磁盘和其他驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆