Inno Setup 中 AppData\LocalLow 的常量?

Constant for AppData\LocalLow in Inno Setup?

目前要访问 LocalLow 我使用这个:

{%USERPROFILE}\AppData\LocalLow

但我想知道 Inno Setup 中是否有一个常量,因为 RoamingLocal 都有一个。

AppData\LocalLow没有常量。

您可以使用 Pascal 脚本来解决它。

要解析 "LocalLow",必须使用 SHGetKnownFolderPath
另见 Detect the location of AppData\LocalLow.

由于在 Unicode Inno Setup 中缺少(宽)PChar 类型,因此实施过程涉及到一些黑客攻击。

const
  MAX_PATH = 260;
  AppDataLocalLowGUID = '{A520A1A4-1780-4FF6-BD18-167343C5AF16}';

{ There's no PChar in Unicode Inno Setup, }
{ pretend the function returns a pointer to an Integer }
function SHGetKnownFolderPath(rfid: TGUID; dwFlags: DWORD; hToken: THandle;
  var ppszPath: Integer): Integer;
  external 'SHGetKnownFolderPath@Shell32.dll stdcall';

{ And allow the Integer to be copied to string }
function StrCpy(Dest: string; Source: Integer): Integer;
  external 'StrCpyW@Shlwapi.dll stdcall';

{ And allow the Integer pointer to be released }
procedure CoTaskMemFreeAsInteger(pv: Integer);
  external 'CoTaskMemFree@Ole32.dll stdcall';

function GetAppDataLocalLow: string;
var
  Path: Integer;
  I: Integer;
begin
  if SHGetKnownFolderPath(StringToGUID(AppDataLocalLowGUID), 0, 0, Path) = 0 then
  begin
    { The path should not be longer than MAX_PATH }
    SetLength(Result, MAX_PATH);

    StrCpy(Result, Path);

    CoTaskMemFreeAsInteger(Path);

    { Look for NUL character and adjust the length accordingly }
    SetLength(Result, Pos(#0, Result) - 1);
  end;
end;

如果您需要在非Code部分(Pascal脚本之外)使用路径,您可以使用scripted constant:

[Files]
Source: myfile.txt; DestDir: {code:GetAppDataLocalLow}

并且您需要更改函数签名以采用虚拟参数:

function GetAppDataLocalLow(Param: string): string;

例如,使用 INNO 从 LocalLow 卸载时删除文件:

[UninstallDelete]
Type: filesandordirs; Name: "{userappdata}\..\LocalLow\MyFile.txt"