天天看點

利用Inno Setup在VS編譯時自動建構安裝包

作者:opendotnet

本文由網友投稿,.NET是免費、開源、跨平台的。

作者:傲慢與偏見

原文标題:利用Inno Setup在VS編譯時自動建構安裝包

原文連結:https://www.cnblogs.com/chonglu/p/17566940.html

歡迎網友們投稿技術類文章,題材不限,沒有稿費...

利用Inno Setup在VS編譯時自動建構安裝包

1. 摘要

很多C/S開發領域的同學或多或少都可能會遇到需要制作安裝包的場景,打包的工具也是五花八門,例如有NSIS、InstallShield、Wix Toolset、ClickOnce等等,這裡以Inno Setup為例簡單示範下如何建構安裝包,以及在Visual Studio中編譯程式時自動去建構這個安裝包。

2. 操作示範

Tips:如果是想自動化建構,建議先從文章尾部開始看起,前面隻是簡單示範如何手動建構

2.1. 去官網下載下傳Inno Setup打包工具

點選 Inno Setup Downloads[1] 下載下傳:

利用Inno Setup在VS編譯時自動建構安裝包
利用Inno Setup在VS編譯時自動建構安裝包

下載下傳完手動放進Inno Setup安裝目錄下的Languages目錄下即可

利用Inno Setup在VS編譯時自動建構安裝包

2.2. 建立打包項目

利用Inno Setup在VS編譯時自動建構安裝包

2.3. 填寫應用基本資訊

利用Inno Setup在VS編譯時自動建構安裝包

2.4. 填寫應用安裝目錄

利用Inno Setup在VS編譯時自動建構安裝包

2.5. 在VS中右鍵項目屬性,更改項目輸出路徑,有多個項目則都改為同一個

利用Inno Setup在VS編譯時自動建構安裝包

2.6. 選擇應用需要打包的檔案

利用Inno Setup在VS編譯時自動建構安裝包
  • Application main executable file: 應用程式主程式
  • Other application files: 主程式所依賴的項目或第三方依賴等,這裡建議直接選擇輸出目錄

2.7. 應用檔案類型關聯

利用Inno Setup在VS編譯時自動建構安裝包

若你程式用不上這個功能一般不用勾選。

2.8. 應用程式快捷方式建立

利用Inno Setup在VS編譯時自動建構安裝包

2.9. 應用協定許可檔案

利用Inno Setup在VS編譯時自動建構安裝包

2.10. 安裝模式選擇

利用Inno Setup在VS編譯時自動建構安裝包

2.11. 安裝包語言選擇

利用Inno Setup在VS編譯時自動建構安裝包

官方預設是不提供中文語言包的,但官網上有第三方使用者提供和維護了中文包,有需要的可以在 Inno Setup Translations[3]下載下傳,然後手動放進Inno Setup安裝目錄下的Languages下即可,連結前面也提到過。

2.12. 安裝包設定

利用Inno Setup在VS編譯時自動建構安裝包

2.13. 設定完了就會生成一個腳本

利用Inno Setup在VS編譯時自動建構安裝包

2.14. 編譯完成,則會在目錄生成一個exe檔案,輕按兩下就可以進行安裝

利用Inno Setup在VS編譯時自動建構安裝包

2.15. 安裝效果

利用Inno Setup在VS編譯時自動建構安裝包

至此,一個簡易的安裝包制作完成。

但....這并不是本篇的主題。顯而易見,如果每次程式有改動,就需要去Inno Setup的編輯器手動執行編譯一下生成安裝包,有些繁瑣。

3. 自動化建構安裝包

3.1. 修改剛才建立安裝包時生成的腳本檔案

主要是将絕對路徑改為相對路徑以及自動擷取主程式檔案版本等,避免其它同僚/電腦編譯時報錯,可以參考下我的腳本

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "傲慢與偏見"
#define MyAppPublisher "版權資訊"
#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\中文簡體.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. 把Inno Setup的安裝目錄拷貝一下放到工程目錄

裡面有些無用的可以删掉,減小體積,可以參考以下

利用Inno Setup在VS編譯時自動建構安裝包

3.3. 設定項目主程式生成後事件

利用Inno Setup在VS編譯時自動建構安裝包

參考指令:

IF "$(ConfigurationName)" == "Release" (
"$(SolutionDir)DevOps\InnoSetup\ISCC.exe" "$(SolutionDir)DevOps\InnoSetup\KJTStep.iss"
 start explorer /select,"$(SolutionDir)DevOps\Publish\"
)
           

為防止影響調試,可以加個判斷,隻有Release模式編譯項目則執行指令,該指令的意思就是通過調用解決方案下的ISCC程式傳入安裝包腳本進而建構編譯出一個最新的安裝包,最後再調起檔案資料總管打開安裝包目錄。

4. 效果示範

利用Inno Setup在VS編譯時自動建構安裝包

每次需要釋出新版本時,将解決方案切換為Release模式編譯,F6 Build一下,安裝包就自動生成出來了,這裡隻是抛磚引玉給個思路,可以多閱讀下官方文檔實作出更為完美的安裝包。

Tips:為避免編寫代碼調試時編譯速度過慢,最好還是要在生成後事件中加上Release模式的判斷,當解決方案中有很多個項目時,或編譯目錄依賴檔案過多的情況下,Inno Setup建構的會有點慢。

5. 結語

Visual Studio中的生成事件其實是一個很實用的東西,可以在編譯前、編譯後自動化執行一些經常需要人為手工操作的事,比如還可以利用生成後事件進行代碼混淆,本篇文章就暫時不展開細說了。

如果在實際操作中遇到錯誤或有疑問,以及更好的建議可以在評論中交流..

6. 附錄

  • Inno Setup官網:https://jrsoftware.org/
  • Inno Setup編輯器:https://jrsoftware.org/isdl.php
  • Inno Setup語言包:https://jrsoftware.org/files/istrans/
  • Inno Setup文檔:https://jrsoftware.org/ishelp/

參考

[1]

Inno Setup Downloads: https://jrsoftware.org/isdl.php

[2]

Inno Setup Translations: https://jrsoftware.org/files/istrans/

[3]

Inno Setup Translations: https://jrsoftware.org/files/istrans/

繼續閱讀