使用Inno Setup 打包.NET程序,并自动安装.Net Framework

来源:互联网 发布:艾美依航空制造 知乎 编辑:程序博客网 时间:2024/06/17 01:33

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到安装包方式脚本:

[vb] view plain copy
  1. ; 脚本由 Inno Setup 脚本向导 生成!  
  2. ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!  
  3.  
  4. #define MyAppName "MyApp"  
  5. #define MyAppVersion "1.0"  
  6. #define IncludeFramework true  
  7. #define IsExternal ""  
  8. #define MyAppPublisher "App"  
  9. #define MyAppURL "http://www.MyApp.cn"  
  10. #define MyAppExeName "MyApp.exe"  
  11.   
  12. [Setup]  
  13. ; 注: AppId的值为单独标识该应用程序。  
  14. ; 不要为其他安装程序使用相同的AppId值。  
  15. ; (生成新的GUID,点击 工具|在IDE中生成GUID。)  
  16. AppId={{B0C52F2E-939F-4CE2-89F3-2F0677584526}  
  17. AppName={#MyAppName}  
  18. AppVersion={#MyAppVersion}  
  19. ;AppVerName={#MyAppName} {#MyAppVersion}  
  20. AppPublisher={#MyAppPublisher}  
  21. AppPublisherURL={#MyAppURL}  
  22. AppSupportURL={#MyAppURL}  
  23. AppUpdatesURL={#MyAppURL}  
  24. DefaultDirName={pf}\{#MyAppName}  
  25. DefaultGroupName={#MyAppName}  
  26. OutputDir=E:\step  
  27. Compression=lzma  
  28. SolidCompression=yes  
  29. #if IncludeFramework  
  30.   OutputBaseFilename=setup_FW  
  31. #else  
  32.   OutputBaseFilename=Setup  
  33. #endif  
  34.   
  35. [Languages]  
  36. Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"  
  37.   
  38. [Tasks]  
  39. Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 0,6.1  
  40.   
  41. [Files]  
  42. Source: "E:\MyApp\MyApp.exe"; DestDir: "{app}"; Flags: ignoreversion {#IsExternal}  
  43. #if IncludeFramework  
  44. Source: "D:\开发\dotNetFx40_Full_x86_x64.exe"; DestDir: "{tmp}"; Flags: ignoreversion {#IsExternal}; Check: NeedsFramework  
  45. #endif  
  46. ; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”  
  47.   
  48. [Icons]  
  49. Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"  
  50. Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon  
  51.   
  52.   
  53. [Run]  
  54. #if IncludeFramework  
  55. Filename: {tmp}\dotNetFx40_Full_x86_x64.exe; Parameters: "/q:a /c:""install /l /q"""; WorkingDir: {tmp}; Flags: skipifdoesntexist; StatusMsg: "Installing .NET Framework if needed"  
  56. #endif  
  57. 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..."  
  58. 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..."  
  59.   
  60. [UninstallRun]  
  61. Filename: {win}\Microsoft.NET\Framework\v4.0.30319\CasPol.exe; Parameters: "-q -machine -remgroup ""{#MyAppName}"""; Flags: skipifdoesntexist runhidden;  
  62.   
  63. [code]  
  64. // Indicates whether .NET Framework 2.0 is installed.  
  65. function IsDotNET40Detected(): boolean;  
  66. var  
  67.     success: boolean;  
  68.     install: cardinal;  
  69. begin  
  70.     success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Install', install);  
  71.      //success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727', 'Install', install);  
  72.     Result :=  success and (install = 1);  
  73. end;  
  74.   
  75. //RETURNS OPPOSITE OF IsDotNet20Detected FUNCTION  
  76. //Remember this method from the Files section above  
  77. function NeedsFramework(): Boolean;  
  78. begin  
  79.   Result := (IsDotNET40Detected = false);  
  80. end;  
  81.   
  82.   
  83.   
  84. function GetCustomSetupExitCode(): Integer;  
  85. begin  
  86.   if (IsDotNET40Detected = false) then  
  87.     begin  
  88.       MsgBox('.NET Framework 未能正确安装!',mbError, MB_OK);  
  89.       result := -1  
  90.     end  
  91. end;  
  92.   
  93. //卸载程序  
  94. function InitializeUninstall(): Boolean;  
  95. begin  
  96.   Result := MsgBox('卸载程序:' #13#13 '你真的要卸载该程序?', mbConfirmation, MB_YESNO) = idYes;  
  97.   //if Result = False then  
  98.   //  MsgBox('InitializeUninstall:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);  
  99. end;  
  100.   
  101.   
  102. procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);  
  103. var  
  104.   ErrorCode: Integer;  
  105. begin  
  106.   case CurUninstallStep of  
  107.     usUninstall:  
  108.       begin  
  109.         //MsgBox('卸载程序:' #13#13 '正在卸载...', mbInformation, MB_OK)  
  110.         // ...insert code to perform pre-uninstall tasks here...  
  111.       end;  
  112.     usPostUninstall:  
  113.       begin  
  114.         //MsgBox('卸载程序:' #13#13 '卸载完成.', mbInformation, MB_OK);  
  115.         // ...insert code to perform post-uninstall tasks here...  
  116.         ShellExec('open', 'http://www.asiafinance.cn', '', '', SW_SHOW, ewNoWait, ErrorCode)  
  117.       end;  
  118.   end;  
  119. end;  

脚本说明:

卸载完成之后会自动打开网页,其中的逻辑可以在里面自由扩展。

检测哪个Framwork可以在注册表中找到这个节点。

Inno Setup在线下载并安装.NetFramwork

[vb] view plain copy
  1. ; 脚本由 Inno Setup 脚本向导 生成!  
  2. ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!  
  3.  
  4. #define MyAppName "MyApp"  
  5. #define MyAppVersion "1.0"  
  6. #define MyAppPublisher "MyApp"  
  7. #define MyAppURL "http://www.MyApp.cn/"  
  8. #define MyAppExeName "MyApp.exe"  
  9.   
  10. [Setup]  
  11. ; 注: AppId的值为单独标识该应用程序。  
  12. ; 不要为其他安装程序使用相同的AppId值。  
  13. ; (生成新的GUID,点击 工具|在IDE中生成GUID。)  
  14. AppId={{769CC8AC-50C3-4776-95F5-A1ABF15A38F4}  
  15. AppName={#MyAppName}  
  16. AppVersion={#MyAppVersion}  
  17. ;AppVerName={#MyAppName} {#MyAppVersion}  
  18. AppPublisher={#MyAppPublisher}  
  19. AppPublisherURL={#MyAppURL}  
  20. AppSupportURL={#MyAppURL}  
  21. AppUpdatesURL={#MyAppURL}  
  22. DefaultDirName={pf}\{#MyAppName}  
  23. DefaultGroupName={#MyAppName}  
  24. OutputDir=E:\step  
  25. OutputBaseFilename=MyApp  
  26. Compression=lzma  
  27. SolidCompression=yes  
  28.   
  29. [Languages]  
  30. Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"  
  31.   
  32. [Tasks]  
  33. Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked; OnlyBelowVersion: 0,6.1  
  34.   
  35. [Files]  
  36. Source: C:\Program Files\ISTool\isxdl.dll; Flags: dontcopy ;  
  37. Source: "E:\MyApp\MyApp.exe"; DestDir: "{app}"; Flags: ignoreversion  
  38. ; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”  
  39.   
  40. [Icons]  
  41. Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"  
  42. Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"  
  43. Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon  
  44.   
  45. [Run]  
  46. Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, "&", "&&")}}"; Flags: nowait postinstall skipifsilent  
  47.   
  48. [Code]  
  49. var  
  50.   dotnetRedistPath: string;  
  51.   downloadNeeded: boolean;  
  52.   dotNetNeeded: boolean;  
  53.   memoDependenciesNeeded: string;  
  54.   
  55. procedure isxdl_AddFile(URL, Filename: PChar);  
  56. external 'isxdl_AddFile@files:isxdl.dll stdcall';  
  57. function isxdl_DownloadFiles(hWnd: Integer): Integer;  
  58. external 'isxdl_DownloadFiles@files:isxdl.dll stdcall';  
  59. function isxdl_SetOption(Option, Value: PChar): Integer;  
  60. external 'isxdl_SetOption@files:isxdl.dll stdcall';  
  61. const  
  62.   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';  
  63.   
  64. //this url was correct at time of publication for .net 3.5 you may need to change this in future.  
  65.   // local system for testing…  
  66.   // dotnetRedistURL = ‘http://192.168.1.1/dotnetfx35.exe’;  
  67.   
  68. function InitializeSetup(): Boolean;  
  69.   
  70. begin  
  71.   Result := true;  
  72.   dotNetNeeded := false;  
  73.   
  74.   // Check for required netfx installation  
  75.   if (not RegKeyExists(HKLM, 'Software\Microsoft\.NETFramework\policy\v4.0')) then begin  
  76.     dotNetNeeded := true;  
  77.     if (not IsAdminLoggedOn()) then begin  
  78.       MsgBox('GasSoft needs the Microsoft .NET Framework to be installed by an Administrator', mbInformation, MB_OK);  
  79.       Result := false;  
  80.     end else begin  
  81.       memoDependenciesNeeded := memoDependenciesNeeded + '.NET Framework' #13;  
  82.       dotnetRedistPath := ExpandConstant('{src}\dotnetfx35.exe');  
  83.       if not FileExists(dotnetRedistPath) then begin  
  84.         dotnetRedistPath := ExpandConstant('{tmp}\dotnetfx35.exe');  
  85.         if not FileExists(dotnetRedistPath) then begin  
  86.           isxdl_AddFile(dotnetRedistURL, dotnetRedistPath);  
  87.           downloadNeeded := true;  
  88.         end;  
  89.       end;  
  90.       SetIniString('install', 'dotnetRedist', dotnetRedistPath, ExpandConstant('{tmp}\dep.ini'));  
  91.     end;  
  92.   end;  
  93. end;  
  94.   
  95. function NextButtonClick(CurPage: Integer): Boolean;  
  96. var  
  97.   hWnd: Integer;  
  98.   ResultCode: Integer;  
  99.   
  100. begin  
  101.   Result := true;  
  102.   
  103.   if CurPage = wpReady then begin  
  104.           if (not RegKeyExists(HKLM, 'Software\Microsoft\.NETFramework\policy\v4.0')) then begin  
  105.     hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));  
  106.   
  107.     // don’t try to init isxdl if it’s not needed because it will error on < ie 3  
  108.     if downloadNeeded then begin  
  109.   
  110.       isxdl_SetOption('label', '正在下载 Microsoft .NET Framework');  
  111.       isxdl_SetOption('des-c-r-i-p-tion', '您还未安装Microsoft .NET Framework. 请您耐心等待,下载完成后会安装到您的的计算机中。');  
  112.       if isxdl_DownloadFiles(hWnd) = 0 then Result := false;  
  113.     end;  
  114.     if (Result = true) and (dotNetNeeded = true) then begin  
  115.       if Exec(ExpandConstant(dotnetRedistPath), '/qb', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin  
  116.          // handle success if necessary; ResultCode contains the exit code  
  117.          if not (ResultCode = 0) then begin  
  118.            Result := false;  
  119.          end;  
  120.       end else begin  
  121.          // handle failure if necessary; ResultCode contains the error code  
  122.          Result := false;  
  123.       end;  
  124.       end;  
  125.     end;  
  126.   end;  
  127. end;  

我检测是.Net Framework4.0,安装包大小大概48M,相比3.0还是小了不少.

在线安装的话安装包是小了,但是下载缺是比较耗时的。

阅读全文
0 0
原创粉丝点击