You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Inno Setup中实现自动查找Steam的sourcemods目录

在Inno Setup中自动定位Steam安装目录并安装至steamapps/sourcemods

核心思路

Steam的安装路径通常会写入系统注册表,这是最可靠的获取方式;如果注册表无记录,再通过遍历常见盘符的默认路径兜底查找。找到Steam根目录后,直接拼接出steamapps/sourcemods作为目标安装路径。

实现代码

在脚本的[Code]段添加以下逻辑:

function GetSteamInstallPath: String;
var
  RegPath: String;
  Is64Bit: Boolean;
  Drive: Char;
  TestPath: String;
begin
  Result := '';
  Is64Bit := IsWin64;

  // 优先从注册表读取Steam安装路径
  if Is64Bit then
    RegPath := 'SOFTWARE\Wow6432Node\Valve\Steam'
  else
    RegPath := 'SOFTWARE\Valve\Steam';

  if RegQueryStringValue(HKLM, RegPath, 'InstallPath', Result) then
  begin
    // 验证路径是否实际存在
    if not DirExists(Result) then
      Result := '';
  end;

  // 注册表未找到时,遍历盘符查找常见Steam路径
  if Result = '' then
  begin
    for Drive := 'C' to 'Z' do
    begin
      // 检查多种可能的Steam安装路径
      TestPath := Drive + ':\Steam';
      if DirExists(TestPath) then
      begin
        Result := TestPath;
        Break;
      end;
      TestPath := Drive + ':\Program Files\Steam';
      if DirExists(TestPath) then
      begin
        Result := TestPath;
        Break;
      end;
      TestPath := Drive + ':\Program Files (x86)\Steam';
      if DirExists(TestPath) then
      begin
        Result := TestPath;
        Break;
      end;
      TestPath := Drive + ':\SteamLibrary';
      if DirExists(TestPath) then
      begin
        Result := TestPath;
        Break;
      end;
    end;
  end;

  // 拼接sourcemods目标路径
  if Result <> '' then
    Result := AddBackslash(Result) + 'steamapps\sourcemods';
end;

procedure InitializeWizard;
var
  TargetPath: String;
begin
  TargetPath := GetSteamInstallPath;
  if TargetPath <> '' then
  begin
    // 设置安装向导默认目录
    WizardForm.DirEdit.Text := TargetPath;
    // 自动创建不存在的sourcemods目录
    if not DirExists(TargetPath) then
      ForceDirectories(TargetPath);
  end
  else
  begin
    // 自动查找失败时提示用户手动选择
    MsgBox('未自动检测到Steam安装目录,请手动选择安装路径。', mbInformation, MB_OK);
  end;
end;

关键说明

  • 注册表兼容:区分32位/64位系统,64位系统下Steam注册表项位于Wow6432Node分支,避免读取不到路径。
  • 路径有效性校验:读取注册表后会验证路径是否存在,防止无效路径被使用。
  • 兜底逻辑:遍历所有盘符的常见Steam安装位置,覆盖用户自定义安装路径的场景。
  • 目录预创建:自动生成steamapps/sourcemods目录,确保安装路径可用。
  • 用户交互:自动查找失败时,弹出提示引导用户手动选择路径。

脚本整合

将上述代码加入你的Inno Setup脚本,同时在[Setup]段设置默认目录为动态获取的路径:

[Setup]
DefaultDirName={code:GetSteamInstallPath}

这样安装程序启动时会自动填充找到的目标路径。

内容的提问来源于stack exchange,提问作者Said

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:51:14