NSIS play with InstalldirRegKey

2019-08-08 08:58发布

问题:

From NSIS documentation, we have

root_key subkey key_name This attribute tells the installer to check a string in the registry, and use it for the install dir if that string is valid. If this attribute is present, it will override the InstallDir attribute if the registry key is valid, otherwise it will fall back to the InstallDir default.

So, if I have these lines in a .nsi file:

InstallDir "D:\myFolder\myFile"

InstallDirRegKey HKCU "Software\${PRODUCT_COMPANY}\${PRODUCT_NAME}" "Install_Dir"

I understand that I set my install directory to D:\myFolder\myFile as default directory, but if Software\${PRODUCT_COMPANY}\${PRODUCT_NAME} is a valid path, then I will use it instead. Is that correct?

From another post, I understand that the InstallDirRegKey instruction is used to overwrite previous installation at same location. How is it working more precisely? If I have the following,

InstallDir "D:\myFolder\myFile"

InstallDirRegKey HKCU "D:\myFolder\myFile" "Install_Dir"

will a new .exe file generated by the NSIS script overwrite the previous one? Or does it mean that the executable will overwrite the previous one?

回答1:

InstallDirRegKey only reads from the registry, it never writes.

Before your .onInit is executed NSIS does:

  1. If InstallDir is set, that path is copied to $Instdir
  2. If InstallDirRegKey is set and the registry key exists, the path from the registry (With the filename removed) is copied to $Instdir

So if you want InstallDirRegKey to have any effect the next time a user runs the installer you must point it to a key that your installer creates in one of your sections. It can be the UninstallString command in your uninstall key or a application specific key like HKLKM\Software\My Company\My App.

The whole point of this is that when the user re-installs or installs a new version of your app it will be installed in the same folder (overwriting/upgrading the existing install).