当前位置: 首页 > 工具软件 > NSIS > 使用案例 >

NSIS 简单教程

宋成天
2023-12-01

NSIS Setup

如果这个页面是您第一次使用NSIS,那么您将需要NSIS编译器来将以下脚本和您创建的其他脚本转换为有效的安装程序。您可以使用NSIS菜单,并在编译器部分下单击编译NSI脚本启动MakeNSISW。

NSIS安装文件夹中的makensisw.exe是实际的编译器。它有一个图形化的前端,解释了三种加载脚本的方法,所以它非常容易使用。安装NSIS之后,要创建一个安装程序,需要将脚本复制到文本编辑器中,保存扩展名为.nsi的文件,并将该文件加载到makensisw编译器中。

最简代码

# name the installer
OutFile "Installer.exe"
 
# default section start; every NSIS script has at least one section.
Section
 
# default section end
SectionEnd

简单的Hello World-弹出框

当安装程序运行时,这个hello world脚本将创建一个带有单词“hello world”和一个“OK”按钮的弹出框

# set the name of the installer
Outfile "hello world.exe"
 
# create a default section.
Section
 
# create a popup box, with an OK button and the text "Hello world!"
MessageBox MB_OK "Hello world!"
 
SectionEnd

简单的Hello World-写文本到文件

当安装程序运行时,这个hello world脚本将把“hello world”写入一个文本文件

# declare name of installer file
Outfile "hello world.exe"
 
# open section
Section
 
# create a popup box, with an OK button and some text
MessageBox MB_OK "Now We are Creating Hello_world.txt at Desktop!"
 
/* open an output file called "Hello_world.txt", 
on the desktop in write mode. This file does not need to exist 
before script is compiled and run */
 
FileOpen $0 "$DESKTOP\Hello_world.txt" w
 
# write the string "hello world!" to the output file
FileWrite $0 "hello world!"
 
# close the file
FileClose $0
# Show Success message.
MessageBox MB_OK "Hello_world.txt has been created successfully at Desktop!"
 
 
# end the section
SectionEnd

安装一个文件

这个安装程序脚本将复制文件"test.txt"到安装目录。首先,在与下面安装程序脚本相同的目录中创建test.txt文件,然后编译安装程序脚本。如果安装程序脚本在Desktop上,请在运行编译后的安装程序之前删除test.txt文件。运行简单安装程序将test.txt文件安装到桌面。

# define the name of the installer
Outfile "simple installer.exe"
 
# define the directory to install to, the desktop in this case as specified  
# by the predefined $DESKTOP variable
InstallDir $DESKTOP
 
# default section
Section
 
# define the output path for this file
SetOutPath $INSTDIR
 
# define what to install and place it in the output path
File test.txt
 
SectionEnd

安装一个文件并创建一个卸载程序来删除它

这个脚本将执行以下操作:

  • 创建一个名为“installer.exe”的安装程序;
  • 在桌面上安装一个名为“test.txt”的文件;
  • 在桌面上创建一个名为“uninstall .exe”的卸载程序。
  • 卸载程序将删除自己和已安装的文本文件。
# define installer name
OutFile "installer.exe"
 
# set desktop as install directory
InstallDir $DESKTOP
 
# default section start
Section
 
# define output path
SetOutPath $INSTDIR
 
# specify file to go in output path
File test.txt
 
# define uninstaller name
WriteUninstaller $INSTDIR\uninstaller.exe
 
 
#-------
# default section end
SectionEnd
 
# create a section to define what the uninstaller does.
# the section will always be named "Uninstall"
Section "Uninstall"
 
# Always delete uninstaller first
Delete $INSTDIR\uninstaller.exe
 
# now delete installed file
Delete $INSTDIR\test.txt
 
# Delete the directory
RMDir $INSTDIR
SectionEnd

创建一个开始菜单项

这个安装程序创建了一个开始菜单项,仅此而已

# Name the installer
OutFile "installer.exe"
 
# default section
Section
 
    # create a shortcut named "new shortcut" in the start menu programs directory
    # presently, the new shortcut doesn't call anything (the second field is blank)
    CreateShortcut "$SMPROGRAMS\new shortcut.lnk" ""
 
    # to delete shortcut, go to start menu directory and manually delete it
 
# default sec end
SectionEnd

带有开始菜单项的安装程序和卸载程序

这个安装程序将执行以下操作:

  • 创建一个名为“installer.exe”的安装程序;
  • 将安装在桌面的卸载程序;
  • 和开始菜单中指向卸载程序的快捷方式。
# define name of installer
OutFile "installer.exe"
 
# define installation directory
InstallDir $DESKTOP
 
# For removing Start Menu shortcut in Windows 7
RequestExecutionLevel user
 
# start default section
Section
 
    # set the installation directory as the destination for the following actions
    SetOutPath $INSTDIR
 
    # create the uninstaller
    WriteUninstaller "$INSTDIR\uninstall.exe"
 
    # create a shortcut named "new shortcut" in the start menu programs directory
    # point the new shortcut at the program uninstaller
    CreateShortcut "$SMPROGRAMS\new shortcut.lnk" "$INSTDIR\uninstall.exe"
SectionEnd
 
# uninstaller section start
Section "uninstall"
 
    # first, delete the uninstaller
    Delete "$INSTDIR\uninstall.exe"
 
    # second, remove the link from the start menu
    Delete "$SMPROGRAMS\new shortcut.lnk"
 
    RMDir $INSTDIR
# uninstaller section end
SectionEnd

获取Java运行时环境的当前版本

这个安装程序只是使用ReadRegStr检查本地机器注册表中JRE的CurrentVersion字符串的值。如果结果为空,说明可能没有安装JRE。

# name the installer
OutFile "installer.exe"
 
#default section start
Section
 
    # read the value from the registry into the $0 register
    ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion
 
    # print the results in a popup message box
    MessageBox MB_OK "version: $0"
 
# default section end
SectionEnd

检查user是否为administrator

有时,有必要检查安装程序的用户是否具有管理权限。这个简单的脚本使用“UserInfo”插件进行检查。在 这个网页 有一个更复杂的替代方法,但这个方法似乎可以达到目的。UserInfo的另一个例子可以在NSIS的安装目录\Examples\UserInfo\UserInfo.nsi下找到。

# name installer
OutFile "installer.exe"
 
# default section start
Section
 
    # call UserInfo plugin to get user info.  The plugin puts the result in the stack
    UserInfo::getAccountType
   
    # pop the result from the stack into $0
    Pop $0
 
    # compare the result with the string "Admin" to see if the user is admin.
    # If match, jump 3 lines down.
    StrCmp $0 "Admin" +3
 
    # if there is not a match, print message and return
    MessageBox MB_OK "not admin: $0"
    Return
 
    # otherwise, confirm and return
    MessageBox MB_OK "is admin"
 
# default section end
SectionEnd

原文参考

 类似资料: