Gowrisankar
Gowrisankar

Reputation: 61

NSIS - How to set an environment variable in system variable

I want to set the environment variable in system variable path for my application. How can I set that using nsis?

For example: C:\Program Files\Sample\bin, I want to set this path in System variable.

Upvotes: 3

Views: 10833

Answers (3)

DrMarbuse
DrMarbuse

Reputation: 870

  1. For simple variables holding only one value or one directory see
    http://nsis.sourceforge.net/Setting_Environment_Variables#Exisiting_Variables
  2. For variables that hold path-lists see
    http://nsis.sourceforge.net/Environmental_Variables:_append%2C_prepend%2C_and_remove_entries#Function_Code

However the second solution requires inclusion of a .nsh-file and it is not clear how to completely reset a variable. It rather has the functionality to remove, append or prepend to a semicolon-seperated list of entries (such as the PATH-Variable).

Upvotes: 0

ehambright
ehambright

Reputation: 1593

There are a couple of ways to do this. I assume you want to set this permanently and not just for the installer

You could use ns::Exec or ExecWait to run setx as mentioned by the other answer. The downside to this is that setx only comes with Windows Vista or later. Windows XP users would not have their path set. The SET command is for the process only and would not permanently set the path.

You could use ReadEnvString to read the current path, format it how you wanted, and then output it back out with WriteRegExpandStr. See http://nsis.sourceforge.net/Setting_Environment_Variables. The downside to this is you have to do all your formatting of the path yourself, and you have to duplicate work that's already been done for you in the EnvVarUpdate function - mentioned below...

The method I use is the EnvVarUpdate function. You can find the code for this function here; http://nsis.sourceforge.net/Environmental_Variables:_append,_prepend,_and_remove_entries

Using this function with your example path it would be;

${EnvVarUpdate} $0 "PATH" "A" "HKLM" "C:\Program Files\Sample\bin" ; appends to the system path

${EnvVarUpdate} $0 "PATH" "A" "HKCU" "C:\Program Files\Sample\bin" ; appends to the user path

This allows you to choose to append or prepend, and it allows you to remove what you added in your uninstaller.

Note that NSIS with the default compiled binaries has a maximum string length of 1024 characters. If the path is longer than that, you can corrupt it if you don't apply the patch listed in the above link. The preferred solution would be to download the binary of makensis which has the string length set to 8192 characters, or compile the source yourself and set a longer string length. You can find more info here; http://nsis.sourceforge.net/Special_Builds

Upvotes: 4

zenpoy
zenpoy

Reputation: 20126

You can use set to add/change an environment variable or you could use setx to set one permanently (as if you added it via computer properties).

here's the documentation for setx and set

Upvotes: 3

Related Questions