This article is contributed by a user. .NET is free, open-source, and cross-platform.
Author: Pride and Prejudice
Original title: Using Inno Setup to Automatically Build Installers During VS Compilation
Original link: https://www.cnblogs.com/chonglu/p/17566940.html
We welcome users to submit technical articles. There are no restrictions on topics, but there is no payment...

1. Summary
Many developers in the C/S field may encounter scenarios where they need to create an installer. There are various packaging tools, such as NSIS, InstallShield, Wix Toolset, ClickOnce, etc. Here, we use Inno Setup as an example to briefly demonstrate how to build an installer and automatically build it when compiling a program in Visual Studio.
2. Operation Demonstration
Tips: If you want to automate the build, it's recommended to start reading from the end of the article. The beginning simply demonstrates how to build manually.
2.1. Download the Inno Setup packaging tool from the official website
Click Inno Setup Downloads to download:

If you need Chinese language support, you need to download the Chinese language pack. Download address: Inno Setup Translations

After downloading, manually place it into the Languages directory under the Inno Setup installation directory.

2.2. Create a packaging project

2.3. Fill in basic application information

2.4. Fill in the application installation directory

2.5. In VS, right-click project properties, change the project output path. If there are multiple projects, change them all to the same one.

2.6. Select the files to be packaged

- Application main executable file: the main program of the application
- Other application files: project dependencies or third-party dependencies required by the main program. It is recommended to directly select the output directory.
2.7. File type associations

If your program does not use this feature, you generally do not need to check this.
2.8. Create application shortcuts

2.9. License agreement file

2.10. Installation mode selection

2.11. Language selection for the installer

The official version does not include a Chinese language pack by default, but there are third-party Chinese language packs maintained by users on the official site. If needed, you can download them from Inno Setup Translations and manually place them into the Languages directory under the Inno Setup installation directory. The link was mentioned earlier.
2.12. Installer settings

2.13. After setting up, a script is generated

2.14. After compilation, an exe file is generated in the directory. Double-click it to start the installation.

2.15. Installation effect

At this point, a simple installer is created.
But... this is not the main topic of this article. Obviously, if the program changes every time, you need to manually compile in the Inno Setup editor to generate the installer, which is somewhat cumbersome.
3. Automated Build of Installer
3.1. Modify the script file generated when creating the installer
The main idea is to change absolute paths to relative paths and automatically obtain the main program file version to avoid errors when other colleagues/computer compile. You can refer to my script.
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "Pride and Prejudice"
#define MyAppPublisher "Copyright Information"
#define MyAppURL "https://www.cnblogs.com/chonglu"
#define MyAppExeName "Inno SetupSample.exe"
#define MyAppVersion GetVersionNumbersString("..\output\Inno SetupSample.exe")
[Setup]
; #define MyAppVersion GetVersionNumbersString("E:\01-项目代码\YZS\KJT\trunk\KJT\bin\KJT.exe")
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{72EC6D66-B10E-4E61-920F-86852D3FFA91}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\KJT
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputBaseFilename=KJTStep
SetupIconFile=AppICon.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
OutputDir=..\Publish
[Languages]
Name: "Chinese"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"
[Files]
Source: "..\bin\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\bin\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
3.2. Copy the installation directory of Inno Setup to the project directory
You can delete some useless files to reduce size. Refer to the following:

3.3. Set post-build events for the main project

Reference command:
IF "$(ConfigurationName)" == "Release" (
"$(SolutionDir)DevOps\InnoSetup\ISCC.exe" "$(SolutionDir)DevOps\InnoSetup\KJTStep.iss"
start explorer /select,"$(SolutionDir)DevOps\Publish\"
)
To avoid affecting debugging, you can add a condition so that the command is only executed when compiling in Release mode. This command calls the ISCC program under the solution directory, passes in the installer script, builds and compiles the latest installer, and finally opens the file explorer to the installer directory.
4. Effect Demonstration

Every time a new version needs to be released, switch the solution to Release mode and compile. Press F6 to build, and the installer is automatically generated. This is just a simple example to give you an idea. You can read the official documentation to create a more perfect installer.
Tips: To avoid slow compilation when writing and debugging code, it is best to add a Release mode condition in the post-build event. When there are many projects in the solution or too many dependency files in the compilation directory, Inno Setup may build slowly.
5. Conclusion
Build events in Visual Studio are very practical. They can automate tasks that are often done manually before or after compilation, such as code obfuscation. This article will not expand on that for now.
If you encounter errors, have questions, or have better suggestions during actual operation, feel free to comment.
6. Appendix
- Inno Setup official website: https://jrsoftware.org/
- Inno Setup editor: https://jrsoftware.org/isdl.php
- Inno Setup language packs: https://jrsoftware.org/files/istrans/
- Inno Setup documentation: https://jrsoftware.org/ishelp/