使用Inno Setup 打包.NET程序,並自動安裝.Net Framework


Inno Setup是什么

Inno Setup 是一個windows系統下的安裝包制作程序。它是免費的(而且允許免費用於商業用途)。官網網站:http://www.jrsoftware.org/

雖然說.NET 可以使用VS來打包安裝包,但可定制化絕對不如Inno Setup,基本的使用這里不做過多介紹。大家Google一下吧。

我們在開發.net客戶端時候.Net Framework是個比較讓人頭疼的問題,比如一個WPF程序大小幾百K,卻要安裝一個幾十M的.Net Framework。但是也沒辦法.這里提供兩種方式,一個是將.Net Framework打包進安裝包中,一個是在線下載.Net Framework然后安裝。

各有各的缺點和優點。大家自己擇優選擇吧。

Inno Setup打包.Net Framework到安裝包方式腳本:

  
  
  
          
; 腳本由 Inno Setup 腳本向導 生成! ; 有關創建 Inno Setup 腳本文件的詳細資料請查閱幫助文檔! #define MyAppName "MyApp" #define MyAppVersion "1.0" #define IncludeFramework true #define IsExternal "" #define MyAppPublisher "App" #define MyAppURL "http: // www.MyApp.cn" #define MyAppExeName "MyApp.exe" [Setup] ; 注: AppId的值為單獨標識該應用程序。 ; 不要為其他安裝程序使用相同的AppId值。 ; (生成新的GUID,點擊 工具 | 在IDE中生成GUID。) AppId = {{B0C52F2E - 939F - 4CE2 - 89F3 - 2F0677584526} AppName = {#MyAppName} AppVersion = {#MyAppVersion} ;AppVerName = {#MyAppName} {#MyAppVersion} AppPublisher = {#MyAppPublisher} AppPublisherURL = {#MyAppURL} AppSupportURL = {#MyAppURL} AppUpdatesURL = {#MyAppURL} DefaultDirName = {pf}\{#MyAppName} DefaultGroupName = {#MyAppName} OutputDir = E:\step Compression = lzma SolidCompression = yes #if IncludeFramework OutputBaseFilename = setup_FW #else OutputBaseFilename = Setup #endif [Languages] Name: " chinesesimp " ; MessagesFile: " compiler:Default.isl " [Tasks] Name: " desktopicon " ; Description: " {cm:CreateDesktopIcon} " ; GroupDescription: " {cm:AdditionalIcons} " ; Flags: unchecked ; OnlyBelowVersion: 0 , 6.1 [Files] Source: " E:\MyApp\MyApp.exe " ; DestDir: " {app} " ; Flags: ignoreversion {#IsExternal} #if IncludeFramework Source: " D:\開發\dotNetFx40_Full_x86_x64.exe " ; DestDir: " {tmp} " ; Flags: ignoreversion {#IsExternal}; Check: NeedsFramework #endif ; 注意: 不要在任何共享系統文件上使用“Flags: ignoreversion” [Icons] Name: " {group}\{#MyAppName} " ; Filename: " {app}\{#MyAppExeName} " Name: " {commondesktop}\{#MyAppName} " ; Filename: " {app}\{#MyAppExeName} " ; Tasks: desktopicon [Run] #if IncludeFramework Filename: {tmp}\dotNetFx40_Full_x86_x64.exe; Parameters: " /q:a /c: "" install /l /q """ ; WorkingDir: {tmp}; Flags: skipifdoesntexist; StatusMsg: " Installing .NET Framework if needed " #endif Filename: {win}\Microsoft.NET\Framework\v4. 0.30319 \CasPol.exe; Parameters: " -q -machine -remgroup "" {#MyAppName} """ ; WorkingDir: {tmp}; Flags: skipifdoesntexist runhidden; StatusMsg: " Setting Program Access Permissions... " Filename: {win}\Microsoft.NET\Framework\v4. 0.30319 \CasPol.exe; Parameters: " -q -machine -addgroup 1.2 -url "" file://{app}/* "" FullTrust -name "" {#MyAppName} """ ; WorkingDir: {tmp}; Flags: skipifdoesntexist runhidden; StatusMsg: " Setting Program Access Permissions... " [UninstallRun] Filename: {win}\Microsoft.NET\Framework\v4. 0.30319 \CasPol.exe; Parameters: " -q -machine -remgroup "" {#MyAppName} """ ; Flags: skipifdoesntexist runhidden; [code] // Indicates whether .NET Framework 2.0 is installed. function IsDotNET40Detected(): boolean; var success: boolean; install: cardinal; begin success : = RegQueryDWordValue(HKLM, ' SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full ' , ' Install ' , install); // success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727', 'Install', install); Result : = success and (install = 1 ); end; // RETURNS OPPOSITE OF IsDotNet20Detected FUNCTION // Remember this method from the Files section above function NeedsFramework(): Boolean; begin Result : = (IsDotNET40Detected = false ); end; function GetCustomSetupExitCode(): Integer; begin if (IsDotNET40Detected = false ) then begin MsgBox( ' .NET Framework 未能正確安裝! ' ,mbError, MB_OK); result : = - 1 end end; // 卸載程序 function InitializeUninstall(): Boolean; begin Result : = MsgBox( ' 卸載程序: ' # 13 # 13 ' 你真的要卸載該程序? ' , mbConfirmation, MB_YESNO) = idYes; // if Result = False then // MsgBox('InitializeUninstall:' #13#13 'Ok, bye bye.', mbInformation, MB_OK); end; procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); var ErrorCode: Integer; begin case CurUninstallStep of usUninstall: begin // MsgBox('卸載程序:' #13#13 '正在卸載...', mbInformation, MB_OK) // ...insert code to perform pre-uninstall tasks here... end; usPostUninstall: begin // MsgBox('卸載程序:' #13#13 '卸載完成.', mbInformation, MB_OK); // ...insert code to perform post-uninstall tasks here... ShellExec( ' open ' , ' http://www.asiafinance.cn ' , '' , '' , SW_SHOW, ewNoWait, ErrorCode) end; end; end;

腳本說明:

卸載完成之后會自動打開網頁,其中的邏輯可以在里面自由擴展。

檢測哪個Framwork可以在注冊表中找到這個節點。

Inno Setup在線下載並安裝.NetFramwork

  
  
  
          
; 腳本由 Inno Setup 腳本向導 生成! ; 有關創建 Inno Setup 腳本文件的詳細資料請查閱幫助文檔! #define MyAppName "MyApp" #define MyAppVersion "1.0" #define MyAppPublisher "MyApp" #define MyAppURL "http: // www.MyApp.cn/" #define MyAppExeName "MyApp.exe" [Setup] ; 注: AppId的值為單獨標識該應用程序。 ; 不要為其他安裝程序使用相同的AppId值。 ; (生成新的GUID,點擊 工具 | 在IDE中生成GUID。) AppId = {{769CC8AC - 50C3 - 4776 - 95F5 - A1ABF15A38F4} AppName = {#MyAppName} AppVersion = {#MyAppVersion} ;AppVerName = {#MyAppName} {#MyAppVersion} AppPublisher = {#MyAppPublisher} AppPublisherURL = {#MyAppURL} AppSupportURL = {#MyAppURL} AppUpdatesURL = {#MyAppURL} DefaultDirName = {pf}\{#MyAppName} DefaultGroupName = {#MyAppName} OutputDir = E:\step OutputBaseFilename = MyApp Compression = lzma SolidCompression = yes [Languages] Name: " chinesesimp " ; MessagesFile: " compiler:Default.isl " [Tasks] Name: " desktopicon " ; Description: " {cm:CreateDesktopIcon} " ; GroupDescription: " {cm:AdditionalIcons} " ; Flags: unchecked ; OnlyBelowVersion: 0 , 6.1 [Files] Source: C:\Program Files\ISTool\isxdl.dll; Flags: dontcopy ; Source: " E:\MyApp\MyApp.exe " ; DestDir: " {app} " ; Flags: ignoreversion ; 注意: 不要在任何共享系統文件上使用“Flags: ignoreversion” [Icons] Name: " {group}\{#MyAppName} " ; Filename: " {app}\{#MyAppExeName} " Name: " {group}\{cm:UninstallProgram,{#MyAppName}} " ; Filename: " {uninstallexe} " Name: " {commondesktop}\{#MyAppName} " ; Filename: " {app}\{#MyAppExeName} " ; Tasks: desktopicon [Run] Filename: " {app}\{#MyAppExeName} " ; Description: " {cm:LaunchProgram,{#StringChange(MyAppName, " & " , " && " )}} " ; Flags: nowait postinstall skipifsilent [Code] var dotnetRedistPath: string ; downloadNeeded: boolean; dotNetNeeded: boolean; memoDependenciesNeeded: string ; procedure isxdl_AddFile(URL, Filename: PChar); external ' isxdl_AddFile@files:isxdl.dll stdcall ' ; function isxdl_DownloadFiles(hWnd: Integer): Integer; external ' isxdl_DownloadFiles@files:isxdl.dll stdcall ' ; function isxdl_SetOption(Option, Value: PChar): Integer; external ' isxdl_SetOption@files:isxdl.dll stdcall ' ; const dotnetRedistURL = ' http://www.microsoft.com/downloads/info.aspx?na=41&srcfamilyid=e5ad0459-cbcc-4b4f-97b6-fb17111cf544&srcdisplaylang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f5%2f6%2f2%2f562A10F9-C9F4-4313-A044-9C94E0A8FAC8%2fdotNetFx40_Client_x86_x64.exe ' ; // this url was correct at time of publication for .net 3.5 you may need to change this in future. // local system for testing… // dotnetRedistURL = ‘ http://192.168.1.1/dotnetfx35.exe ’; function InitializeSetup(): Boolean; begin Result : = true ; dotNetNeeded : = false ; // Check for required netfx installation if (not RegKeyExists(HKLM, ' Software\Microsoft\.NETFramework\policy\v4.0 ' )) then begin dotNetNeeded : = true ; if (not IsAdminLoggedOn()) then begin MsgBox( ' GasSoft needs the Microsoft .NET Framework to be installed by an Administrator ' , mbInformation, MB_OK); Result : = false ; end else begin memoDependenciesNeeded : = memoDependenciesNeeded + ' .NET Framework ' # 13 ; dotnetRedistPath : = ExpandConstant( ' {src}\dotnetfx35.exe ' ); if not FileExists(dotnetRedistPath) then begin dotnetRedistPath : = ExpandConstant( ' {tmp}\dotnetfx35.exe ' ); if not FileExists(dotnetRedistPath) then begin isxdl_AddFile(dotnetRedistURL, dotnetRedistPath); downloadNeeded : = true ; end; end; SetIniString( ' install ' , ' dotnetRedist ' , dotnetRedistPath, ExpandConstant( ' {tmp}\dep.ini ' )); end; end; end; function NextButtonClick(CurPage: Integer): Boolean; var hWnd: Integer; ResultCode: Integer; begin Result : = true ; if CurPage = wpReady then begin if (not RegKeyExists(HKLM, ' Software\Microsoft\.NETFramework\policy\v4.0 ' )) then begin hWnd : = StrToInt(ExpandConstant( ' {wizardhwnd} ' )); // don’t try to init isxdl if it’s not needed because it will error on < ie 3 if downloadNeeded then begin isxdl_SetOption( ' label ' , ' 正在下載 Microsoft .NET Framework ' ); isxdl_SetOption( ' des-c-r-i-p-tion ' , ' 您還未安裝Microsoft .NET Framework. 請您耐心等待,下載完成后會安裝到您的的計算機中。 ' ); if isxdl_DownloadFiles(hWnd) = 0 then Result : = false ; end; if (Result = true ) and (dotNetNeeded = true ) then begin if Exec(ExpandConstant(dotnetRedistPath), ' /qb ' , '' , SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin // handle success if necessary; ResultCode contains the exit code if not (ResultCode = 0 ) then begin Result : = false ; end; end else begin // handle failure if necessary; ResultCode contains the error code Result : = false ; end; end; end; end; end;

我檢測是.Net Framework4.0,安裝包大小大概48M,相比3.0還是小了不少.

在線安裝的話安裝包是小了,但是下載缺是比較耗時的。

 

參考資料:http://zerosandtheone.com/blogs/vb/archive/2008/06/23/vb-net-install-your-app-and-the-net-framework-using-inno-setup.aspx

http://my.hoopchina.com/114266/blog/24775.html


免責聲明!

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



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