# Tutorial: Portable with NSIS
NSIS (Nullsoft Scriptable Install System) is a professional open source system to create Windows installers. It is designed to be as small and flexible as possible and is therefore very suitable for internet distribution. With NSIS, you can create installers that are capable of doing everything that is needed to set up your software, including making it portable.
## What is a portable application?
A portable application is a software that can run on any compatible Windows system without requiring installation or leaving any traces behind. Portable applications are convenient for users who want to carry their software and settings with them on a USB drive or other removable media, or who want to use software without affecting the host system.
## How to create a portable application with NSIS?
To create a portable application with NSIS, you need to write a script that defines the behavior and appearance of your installer. The script is written in a simple but powerful language that allows you to control every aspect of the installation process. You can use variables, functions, macros, plug-ins, and logic statements to customize your installer.
The basic structure of a NSIS script is as follows:
```
; Include files
!include "MUI2.nsh" ; Modern User Interface
!include "LogicLib.nsh" ; Logic Library
; Define constants
Name "My Portable App" ; The name of the installer
OutFile "MyPortableApp.exe" ; The name of the output file
InstallDir "$EXEDIR" ; The default installation directory (the same as the executable directory)
RequestExecutionLevel user ; The requested execution level (user or admin)
; Pages
!insertmacro MUI_PAGE_WELCOME ; The welcome page
!insertmacro MUI_PAGE_LICENSE "License.txt" ; The license page
!insertmacro MUI_PAGE_DIRECTORY ; The directory page
!insertmacro MUI_PAGE_INSTFILES ; The installation files page
!insertmacro MUI_PAGE_FINISH ; The finish page
; Languages
!insertmacro MUI_LANGUAGE "English" ; The default language
; Sections
Section "Main Section" SEC01
SetOutPath $INSTDIR ; Set the output path to the installation directory
File /r "MyApp\*.*" ; Copy all files and subdirectories from MyApp folder to the output path
WriteRegStr HKCU "Software\My Portable App" "Install_Dir" $INSTDIR ; Write the installation directory to the registry
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\My Portable App" "DisplayName" "My Portable App" ; Write the display name to the uninstall registry key
WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\My Portable App" "UninstallString" '"$INSTDIR\Uninstall.exe"' ; Write the uninstall string to the uninstall registry key
WriteUninstaller "$INSTDIR\Uninstall.exe" ; Write the uninstaller to the installation directory
SectionEnd
Section "Start Menu Shortcuts" SEC02
CreateDirectory "$SMPROGRAMS\My Portable App" ; Create a start menu folder for the application
CreateShortCut "$SMPROGRAMS\My Portable App\My Portable App.lnk" "$INSTDIR\MyApp.exe" "" "$INSTDIR\MyApp.exe" 0 ; Create a shortcut for the application
CreateShortCut "$SMPROGRAMS\My Portable App\Uninstall.lnk" "$INSTDIR\Uninstall.exe" "" "$INSTDIR\Uninstall.exe" 0 ; Create a shortcut for the uninstaller
SectionEnd
; Uninstaller section
Section "Uninstall"
DeleteRegKey HKCU "Software\My Portable App" ; Delete the application registry key
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\My Portable App" ; Delete the uninstall registry key
Delete "$SMPROGRAMS\My Portable App\*.*" ; Delete the start menu shortcuts
RMDir "$SMPROGRAMS\My Portable App" ; Delete the start menu folder
Delete "$INSTDIR\*.*" /r ; Delete all files and subdirectories from the installation directory
RMDir "$INSTDIR" ; Delete the installation directory
SectionEnd
```
This script will create a simple installer that will copy your application files to a user-selected directory, create start menu
## How to compile the script?
To compile the script, you need to download and install NSIS from
https://nsis.sourceforge.io/Download. After installation, you will find a program called MakeNSIS in the NSIS folder. You can use this program to compile your script by dragging and dropping it onto the program icon, or by using the command line:
```
makensis.exe "C:\path\to\your\script.nsi"
```
This will create an executable file in the same folder as your script, with the name specified by the OutFile command.
## How to make your application portable?
To make your application portable, you need to ensure that it does not write any data or settings to the system folders or registry, but instead stores them in the same folder as the executable or in a subfolder. You can use relative paths to access these files, such as:
```
$INSTDIR\Data\Settings.ini
```
where $INSTDIR is a variable that holds the installation directory. You can also use macros and plug-ins to read and write data from files or registry keys, such as:
```
!include "FileFunc.nsh"
!insertmacro GetParameters
!insertmacro GetOptions
$GetOptions $CMDLINE "/DataFolder=" $0 ; Get the value of /DataFolder parameter from the command line
$If $0 != "" ; If the parameter is not empty
StrCpy $INSTDIR $0 ; Set the installation directory to the parameter value
$EndIf
$GetINIValue "$INSTDIR\Data\Settings.ini" "General" "Language" $1 ; Get the language value from the INI file
$If $1 == "English" ; If the language is English
!insertmacro MUI_LANGUAGE "English" ; Set the installer language to English
$ElseIf $1 == "Spanish" ; If the language is Spanish
!insertmacro MUI_LANGUAGE "Spanish" ; Set the installer language to Spanish
$EndIf
```
This way, your application will be able to run from any location and keep its settings and data intact.
## How to add a custom icon for the installer and the application?
To add a custom icon for the installer and the application, you need to create an icon file (.ico) with the desired image and size. You can use any image editing software to create an icon file, or use online tools such as
https://icoconvert.com/. After creating the icon file, you need to copy it to the same folder as your script and use the Icon command to specify it in your script, such as:
```
Icon "MyIcon.ico" ; Set the icon for the installer
VIAddVersionKey /LANG=$LANG_ENGLISH "FileDescription" "My Portable App" ; Set the file description for the installer
VIAddVersionKey /LANG=$LANG_ENGLISH "ProductName" "My Portable App" ; Set the product name for the installer
```
To set the icon for the application, you need to use a plug-in called Resource Hacker, which can modify the resources of executable files. You can download Resource Hacker from
https://www.angusj.com/resourcehacker/. After downloading and installing Resource Hacker, you need to copy it to the NSIS\Plugins folder and use it in your script, such as:
```
!include "ResHacker.nsh" ; Include the Resource Hacker plug-in
Section "Main Section" SEC01
SetOutPath $INSTDIR ; Set the output path to the installation directory
File /r "MyApp\*.*" ; Copy all files and subdirectories from MyApp folder to the output path
$ResHacker::SetIcon "$INSTDIR\MyApp.exe" "$INSTDIR\MyApp.exe" "MyIcon.ico" "" "" "" ; Set the icon for the application
SectionEnd
```
This will replace the default icon of your application with your custom icon.
1.1
This is an update for My Portable App.
```
The version tag should contain the version number of your application, the url tag should contain the download link for the update, and the description tag should contain a brief message about the update. If the version number in the XML file is higher than the version number of your application, NSIS Update will prompt the user to download and install the update. 0f8387ec75