How to get the exe path in nsis script?

2.3k Views Asked by At

I am writing an uninstaller for my application, and I need to check from which directory uninstaller is invoked (because I want to restrict uninstallation from any other directory).

I am using built-in variable $EXEPATH but it is giving me C:\Users\MyUser\Local\Temp\~nsu.tmp instead of current directory.

I also used windows API GetModuleFileName using nsis, but it is also giving same output. How to get the current directory for the executable?

2

There are 2 best solutions below

2
On

The temporary directory in the form of C:\Users\MyUser\Local\Temp\~nsu.tmp is probably the directory where the uninstaller is actually running.

To avoid problems with impossibility to remove the uninstaller executable because it is locked while running (so it need a reboot to be actually deleted), it is common to copy the uninstaller .exe to a temporary place and run it from there.

If you do not want the unistaller to fork itself into a temp directory, you can invoke it this way:

ExecWait '"$INSTDIR\uninstaller.exe" /S _?=$INSTDIR'

The trick involves the _? special parameter described in the manual.

0
On

$InstDir in the uninstaller is set to the path where the uninstaller logically exists. Use $InstDir in place of $ExeDir in the uninstaller.

The uninstaller copies itself to %temp% so code like DeleteFile $InstDir\Uninst.exe will work even though Windows does not allow you to delete running .exe files.