首页 > 解决方案 > 我希望在具有最大可用空间的硬盘驱动器上选择 DefaultDirName

问题描述

我正在使用 Inno Setup 创建安装。我想DefaultDirName在可用空间最大的硬盘驱动器上被选中。

标签: inno-setup

解决方案


实现一个脚本常量来选择磁盘。使用GetDriveTypeWinAPI 函数过滤到固定驱动器 ( DRIVE_FIXED) 和查询可用磁盘空间的GetSpaceOnDisk64函数。

[Setup]
DefaultDirName={code:GetDiskWithMostFreeSpace}My Program
[Code]

const
  DRIVE_NO_ROOT_DIR = 1;
  DRIVE_FIXED = 3;
  
function GetDriveType(lpRootPathName: string): UInt;
  external 'GetDriveTypeW@kernel32.dll stdcall';

function GetDiskWithMostFreeSpace(Param: string): string;
var
  I: Integer;
  D: string;
  MostFree: Int64;
  Free, Total: Int64;
  T: UInt;
begin
  Result := '';
  MostFree := -1;
  for I := Ord('A') to Ord('Z') do
  begin
    D := Chr(I) + ':\';
    T := GetDriveType(D);
    if T = DRIVE_NO_ROOT_DIR then
    begin
      Log(Format('Disk %s does not exist', [D]));
    end
      else
    begin
      Log(Format('Disk %s is of type %d', [D, T]));
      if T = DRIVE_FIXED then
      begin
        if not GetSpaceOnDisk64(D, Free, Total) then
        begin
          Log(Format('Cannot obtain free disk space on %s', [D]));
        end
          else
        begin
          Log(Format('Free disk space on %s is %d', [D, Free]));
          if Free > MostFree then
          begin
            MostFree := Free;
            Result := D;
          end;
        end;
      end;
    end;
  end;

  if Result = '' then
  begin
    RaiseException('Did not find any disk');
  end;

  Log(Format('Selected disk %s', [Result]));
end;

使用 可以改进代码GetVolumeInformation,如如何在 Inno Setup 中检查分区类型?


推荐阅读