解决问题:
程序安装后,通过桌面快捷方式运行程序时不需要再右键选择管理员身份运行。
注意:程序安装时需要使用管理员权限进行安装,才能往注册写入信息
;针对当前用户有效
WriteRegStr HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\${MAIN_PROGRAM_NAME}M.exe" "RUNASADMIN"
;针对所有用户有效
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\${MAIN_PROGRAM_NAME}M.exe" "RUNASADMIN"
解释:${MAIN_PROGRAM_NAME}M.exe 是要执行的主程序的名称
示例代码: writeRegedit.nsi
!include nsdialogs.nsh
XPStyle on
!include "MUI.nsh"
!include "WinCore.nsh"
!include "FileFunc.nsh"
!include "nsWindows.nsh"
!include "WinMessages.nsh"
!include "WndSubclass.nsh"
!include "WordFunc.nsh"
!include "x64.nsh"
!define MAIN_PROGRAM_NAME "D:\test.exe"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\Xiaoneng.exe"
InstallDirRegKey HKLM "Software\PRODUCT_INSTALL_DIR" ""
OutFile "writeRegedit.exe"
Name "writeRegedit"
Section "test"
;针对当前用户有效
WriteRegStr HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\${MAIN_PROGRAM_NAME}" "RUNASADMIN"
;针对所有用户有效
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\${MAIN_PROGRAM_NAME}" "RUNASADMIN"
SectionEnd