简体   繁体   中英

Get NSIS Variable for %ALLUSERSPROFILE%

Is there a NSIS variable for %ALLUSERSPROFILE% ?

If not do you know how I can obtain this environment variable using NSIS code?

Note: If I use ReadEnvStr $R7 "ALLUSERSPROFILE" , $R7 contains C:/ProgramData because the installer has requested elevated privileges ( RequestExecutionLevel admin ). This is sooo frustrating!

Starting with Vista %ALLUSERSPROFILE% is %SystemDrive%\\ProgramData. Some of the things that used to be under All Users was moved to %Public% and the rest is in %ProgramData%.

There are several ways to get this directory but they should all give you the same answer:

ReadEnvStr $0 "ALLUSERSPROFILE"
DetailPrint %ALLUSERSPROFILE%=$0


System::Call 'userenv::GetAllUsersProfileDirectory(t.r0,*i${NSIS_MAX_STRLEN})i.r1'
DetailPrint GetAllUsersProfileDirectory=$0


; In Vista+ %ALLUSERSPROFIL% and CSIDL_COMMON_APPDATA is the same directory:
SetShellVarContext all
DetailPrint All:Appdata=$AppData


!define FOLDERID_ProgramData {62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}
System::Call 'shell32::SHGetKnownFolderIDList(g"${FOLDERID_ProgramData}", i0x1000, i0, *i.r1)i.r0'
${If} $0 == 0
    System::Call 'shell32::SHGetPathFromIDList(ir1,t.r0)'
    System::Call 'ole32::CoTaskMemFree(ir1)'
    DetailPrint SHGetKnownFolderIDList=$0
${EndIf}

To expand on @Anders reply, you could also use SHGetSpecialFolderPath and make a simple one line call to receive the path to a folder on the operating system.


System::Call 'shell32::SHGetSpecialFolderPath(i $HWNDPARENT, t .r1, i 0x23, i0)i.r0'

Use it with CSIDL Values , and you can simply call a function and pop the return.

Function ".OnInit"
  System::Call 'shell32::SHGetSpecialFolderPath(i $HWNDPARENT, t .r1, i 0x23, i0)i.r0'
  pop $1
  MessageBox MB_OK|MB_ICONINFORMATION "$1"
FunctionEnd

Section ""
  ; blank section (so the script runs)
SectionEnd

This will return C:\\ProgramData (Vista+) or C:\\Documents and Settings\\All Users\\Application Data (XP) into $1 and display a Message Box showing the path.

By switching up the CSIDL Value ( 0x23 ) you can return the path to a bunch of different system folders.


Here are some common CSIDL Values that you can use to return the path:

  • 0x0 Desktop
  • 0x2 Programs
  • 0x5 My document
  • 0x6 Favorites
  • 0x7 Startup
  • 0x8 Recent documents
  • 0x9 Sendto documents
  • 0x10 Desktop Directory
  • 0x11 My Computer
  • 0x14 Fonts directory
  • 0x15 Windows Template
  • 0x20 Internet Cache
  • 0x21 Cookies
  • 0x22 History
  • 0x23 Common Application Data
  • 0x25 System
  • 0x26 Program Files
  • 0x27 My Pictures
  • 0xb StartMenu
  • 0xd My Music
  • 0x1a Application Data
  • 0x1c Local Application Data
  • 0x2b Common Program Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM