Inno Setup 在所有用户的所有桌面上创建单独的快捷方式

问题描述 投票:0回答:1

我正在使用 Inno Setup 在用户桌面上创建快捷方式:

Name: "{commondesktop}\Setup"; Filename: "{app}\Setup.exe"; WorkingDir: "{pf}\Program"; IconFilename: "{app}\Setup.ico"

但是没有管理员权限的用户是无法删除这个快捷方式的,如何给普通用户授予权限,删除这个图标呢?应在每个用户的桌面上创建图标,但用户应有权删除它。

permissions inno-setup desktop shortcut-file windows-shortcut
1个回答
6
投票

{commondesktop}
快捷方式在公共桌面上共享。所以只有一份快捷方式。

如果您允许用户删除,则当一个用户删除该图标时,其他所有用户都会删除该图标。这就是为什么普通用户不允许修改/删除共享快捷方式。

虽然您可以向所有用户授予该快捷方式的删除权限,但这不是您应该做的。


如果每台机器仅由一个用户使用,请将图标安装到

userdesktop
,而不是
commondestop
。但这仅在该用户(非管理员)实际运行安装程序时才有效。有关此问题的一般讨论,请参阅从以管理员身份运行的 Inno Setup 安装程序为当前登录的用户安装应用程序

没有简单的方法可以将图标安装到所有桌面上。您必须使用 Pascal 脚本并迭代所有配置文件。

简单的方法是迭代

C:\Users
的子文件夹,在每个用户子文件夹的
Desktop
子文件夹中创建快捷方式:

procedure CurStepChanged(CurStep: TSetupStep);
var
  UsersPath: string;
  CommonDesktopShortPath: string;
  DesktopPath: string;
  ShortcutPath: string;
  FindRec: TFindRec;
  ShortcutsCount: Integer;
begin
  // Once the files are installed
  if CurStep = ssPostInstall then
  begin
    Log('Creating shortcuts');
    // Assuming the common users root (typically C:\Users) is two level up
    // from the current user desktop folder
    UsersPath :=
      AddBackslash(ExtractFilePath(RemoveBackslash(ExtractFilePath(
        RemoveBackslash(ExpandConstant('{userdesktop}'))))));
    Log(Format('Users root [%s]', [UsersPath]));
    CommonDesktopShortPath := GetShortName(ExpandConstant('{commondesktop}'));
    Log(Format('Common desktop [%s]', [CommonDesktopShortPath]));

    ShortcutsCount := 0;

    // Iterate all users
    if FindFirst(UsersPath + '*', FindRec) then
    begin
      try
        repeat
          // Just directories, not interested in files
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
          begin
            // Check if there is a Desktop subfolder
            DesktopPath := UsersPath + FindRec.Name + '\Desktop';
            if DirExists(DesktopPath) then
            begin
              if CompareText(
                   CommonDesktopShortPath, GetShortName(DesktopPath)) = 0 then
              begin
                Log(Format('Skipping common desktop [%s]', [DesktopPath]));
              end
                else
              begin
                ShortcutPath := DesktopPath + '\My Program.lnk';
                Log(Format(
                  'Found desktop folder for user [%s], creating shortcut [%s]', [
                  FindRec.Name, ShortcutPath]));
                try
                  ShortcutPath := CreateShellLink(
                    ShortcutPath, 'My Program',
                    ExpandConstant('{app}\MyProg.exe'), '',
                    ExpandConstant('{app}'), '', 0, SW_SHOWNORMAL);
                  Log(Format('Shortcut [%s] created', [ShortcutPath]));
                  Inc(ShortcutsCount);
                except
                  Log(Format('Failed to create shortcut: %s', [
                    GetExceptionMessage]));
                end;
              end;
            end;
          end;
        until not FindNext(FindRec);
      finally
        FindClose(FindRec);
      end;

      Log(Format('%d shortcuts created', [ShortcutsCount]));
    end
      else
    begin
      Log(Format('Error listing [%s]', [UsersPath]));
    end;
  end;
end;

仅当桌面位于本地且位于公共位置时,代码才有效。

如果您需要更强大的解决方案,您可以迭代

中列出的配置文件
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

或者使用 WMI 查询,例如:

SELECT * FROM Win32_UserAccount WHERE localAccount = true and disabled = false

请参阅在 Inno Setup 中查询 Windows 帐户列表

© www.soinside.com 2019 - 2024. All rights reserved.