Archive: Path


Path
How do you make an NSIS installer ad the installation directory to the Windows PATH environment variable?


The default environment variables for the current user are stored at:
HKEY_CURRENT_USER\Environment

For the system (all users) they are stored at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

You can use NSIS' registry functions to read the current value of "PATH", add your directory to it and write the new value back.


Uh....
Could you show me some example code please?


You should start here:
http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2

Maybe try like this:

ReadRegStr $0 HKCU "Environment" "Path"         #read current PATH into $0
StrCpy $0 "C:\SomePath;$0" #prepend your path to the current PATH value
WriteRegStr HKCU "Environment" "Path" '$0' #write back the new PATH


(Note that the new PATH may become effective only after a reboot)

Thanks!! You really are a major dude!!:D I didn't know environment variables were stored in the registry. I need to read up on the registry documentation in the NSIS users manual.


This is not a good idea:

A) Why fill the global path with your crap, there is a total limit, what if all apps did this? It is possible to add per application path info in the registry under "App Paths"

B) Using normal NSIS strings to manipulate global variables is a very bad idea, if the user has a %path% longer than 1024 you will corrupt their setup!


I will only use this if I am writing a command line tool. This allows users of a program to not have to type the full path to the executable.


Originally posted by sethradio
I will only use this if I am writing a command line tool. This allows users of a program to not have to type the full path to the executable.
You should at least ask the user whether he wants your tool to be added to his PATH.

As an alternative you can add a shortcut to the Startmenu that opens a console with your tool added to the PATH, instead of adding your tool to the PATH globally. Many applications, like Visual Studio or GIT, use this technique.

The command for the Startmenu shortcut could be something like:
%comspec% /k ""$INSTDIR\setpath.bat""

And the "setpath.bat" could be created on-the-fly by the installer and should look like:
@echo off
echo Setting up environment for Super Duper Tool...
set "PATH=C:\Path To Your Tool;%PATH%"

How would I remove them
How do I remove the install path from the environment variable in the uninstaller?


Option 1:
Backup the "old" path at install time. Restore it at un-install time. Chnages that have been made since install will be lost!

Option 2:
Read the current PATH at un-install time and use string functions to find "your" path in the current PATH. If found, remove only that part and write the modified PATH back. Harder to do than Option 1, but no changes made by other apps will be lost. However this solution again has the problem that "standard" NSIS can not cope with strings longer than 1024 characters!

Option 3:
Avoid modifying the global PATH environment variable from the very beginning, as has been suggested before in this thread. No problem with un-install and no NSIS string limitations. Probably the cleanest solution that you can go for...


I know I shouldn't use nsis string functions to edit the pah variable at this point. But for the sake of it, how could you use string function to find the install path?


Originally posted by sethradio
I know I shouldn't use nsis string functions to edit the pah variable at this point. But for the sake of it, how could you use string function to find the install path?
You don't have to.

At install time, simply save your $INSTDIR to some (unique) registry key of your own.

At un-install time, read that registry key. No string manipulations will be required.

And, as on Windows the MAX_PATH is 260, the 1024 characters of an NISIS string are sufficient - for a single path.

(Actually the uninstaller will already have $INSTDIR set to the path it was started from)