Inno Setup制作安裝包的幾個問題


系統開發好之后,通常需要制作成安裝包,才能賣給用戶。利用Inno Setup的向導可以制作簡單的安裝包,但是如果要做個好的安裝包的話可能會遇到一些麻煩,今日終於抽空解決了,Inno Setup打包的一些問題。具體如下:

1. 卸載時,如何判斷應用程序是否運行
   InnoSetup 提供變量AppMutex,用來保存應用程序的Mutex名稱。現在很多應用程序都是唯一實例運行。這樣避免配置文件被錯誤修改以及其他很多衍生問題。通常都會用WindowsAPI CreateMuex來創建一個Mutex;安裝包卸載時會判斷AppMutex是否已經被占用。如果被占用則等待並提示用戶關閉應用程序。如果應用程序正在運行,通常該exe文件和被使用的dll是不會被刪除的,卸載不完全。
在Inno Setup Compile 配置中,將MyProgramsMutexName 定義為 "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"    該字符串需要在被打包的EXE文件中使用。
#define MyProgramsMutexName "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"
[Setup]
AppMutex={#MyProgramsMutexName}

2. 如何提示用戶是否刪除配置文件
  Inno Setup提供Script Language支持,可以在[Code] 章節里使用Pascal Script編寫函數 並且Inno Setup提供事件響應。在卸載時,該函數會被調用

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then begin
    if MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) = IDYES
    then  DelFiles();
  end;
end;

這是Inno Setup提供的卸載事件函數,  if CurUninstallStep = usUninstall  判斷是否正在卸載,
如果正在卸載則需判斷是否刪除配置文件,我開發的應用程序使用ini文件存儲配置。MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) 用於彈出消息框,提示用戶選擇是否刪除文件。
函數DelFiles() 為自定義函數,用來刪除文件和目錄。
ExpandConstant用於獲取變量值,FileExists用於判斷文件是否存在;DeleteFile用來刪除文件;
DirExists 用於檢查目錄是否存在;DelTree用於刪除目錄和里面的文件

procedure DelFiles();
begin
  if FileExists(ExpandConstant('{app}') + '\config.ini')  then  DeleteFile(ExpandConstant('{app}') + '\config.ini');
  if FileExists(ExpandConstant('{app}') + '\log.txt')     then DeleteFile(ExpandConstant('{app}') + '\log.txt');
  if DirExists(ExpandConstant('{app}') + '\Users\')       then DelTree(ExpandConstant('{app}') + '\Users\', true, true, true);
end;

Inno Setup 編譯配置文件如下,供參考:

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

#define MyAppName "ityujian"
;應用程序名
#define MyAppVersion "1.0"
;版本號
#define MyAppPublisher "Jianyu Studio"
;發布者名稱
#define MyAppURL "
http://www.cnblogs.com/ityujian/"
;公司的網站
#define MyAppExeName "ityujian.exe"
;exe文件名
#define MyProgramsMutexName "ityujian-A75DEC53-783F-4425-8431-24D83BD4CE5F"
;應用程序中使用的Mutex,卸載時用來判斷應用程序是否正在執行

[Setup]
; 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={{ECCF74CD-48BA-4000-988B-18E965893BD6}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
OutputBaseFilename=setup
SetupIconFile=.\ityujian.ico
;SetupIconFile 是打包之后exe的圖標文件
Compression=lzma
SolidCompression=yes
AppMutex={#MyProgramsMutexName}
;AppMutex是需要打包的應用程序使用的Mutex,卸載之前會先判斷該AppMutex是否已經被占用,如果被占用則提示應用程序正在運行,請關閉

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 0,6.1

[Files]
Source: "GdiPlus.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "ityujian.exe"; DestDir: "{app}"; Flags: ignoreversion
;Source 描述需要打包的EXE文件位置,DestDir描述安裝時該EXE文件的相對位置
Source: "ityujian.pdb"; DestDir: "{app}"; Flags: ignoreversion
Source: "Users\*"; DestDir: "{app}\Users\"; Flags: ignoreversion recursesubdirs createallsubdirs  uninsneveruninstall
;上句用於將Users目錄打進安裝包;uninsneveruninstall說明卸載時,不刪除該文件夾
Source: "Doc\*"; DestDir: "{app}\Doc\"; Flags: ignoreversion recursesubdirs createallsubdirs
;用戶將Doc目錄下的用戶手冊打進安裝包
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon

[UninstallDelete]
Type: dirifempty; Name: "{app}\"
;Type有多種;Type: dirifempty表示如果安裝本應用程序的目錄不為空,則不刪除安裝目錄

[Code]
procedure DelFiles();
begin
  if FileExists(ExpandConstant('{app}') + '\config.ini')  then  DeleteFile(ExpandConstant('{app}') + '\config.ini');
  if FileExists(ExpandConstant('{app}') + '\log.txt')     then DeleteFile(ExpandConstant('{app}') + '\log.txt');
  if DirExists(ExpandConstant('{app}') + '\Users\')       then DelTree(ExpandConstant('{app}') + '\Users\', true, true, true);
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then begin
    if MsgBox('Do you want to delete all config files and log files?', mbConfirmation, MB_YESNO) = IDYES
    then  DelFiles();
  end;
end;

安裝之后截圖:
image

工程 http://files.cnblogs.com/ityujian/ityujian.zip
Inno Setup 去http://www.jrsoftware.org/isinfo.php 下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM