项目inno setup安装脚本,带检测.net framework

本文档介绍了如何使用Inno Setup创建一个安装脚本,该脚本包含检测和安装.NET Framework的功能。脚本涉及检查应用程序是否已安装、检测.NET Framework、卸载旧版本和处理安装过程中的用户交互。此外,还提供了多个参考链接以供进一步学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几乎完全不懂pascal脚本,只是看过这种安装脚本,搞了三天才搞好,各种测试,各种改,终于做出了一个比较完整的安装包。

要说,还是有很多改进的地方,比如没有使用procedure,inno setup可以依照勾选项进行不同的安装步骤,安装完之后可以也可以进行相应的操作,可定制各种安装方式,

以下是我的安装脚本:




; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "appname"
#define MyAppNameEn "appname english"
#define MyAppVersion "2.0.0.0"
#define MyAppPublisher "publisher name, Inc."
#define MyAppExeName "eTMS.exe"
#define MyAppLoggingName "系统登陆"
#define MyAppLoggedName "windows  logged name"
#define RegGridPPBatName "注册控件.bat"
#define RegGridPPName "注册控件"
#define DebugSourcePath "..\Main\bin\x86\Debug"
#define ReleaseSourcePath "..\Main\bin\x86\Release"

[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={{A6A7578-84EC-4006-8226-C87D71FB94}
AppName=" {#MyAppName} [{#MyAppVersion}] "
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\{#MyAppNameEn}
DefaultGroupName=物流云平台
DisableProgramGroupPage=yes
LicenseFile=..\Doc\agreement_chs.txt
InfoBeforeFile=..\Doc\agreement_chs.txt
InfoAfterFile=..\Doc\Readme.txt
OutputDir=..\setup
OutputBaseFilename="{#MyAppName}{#MyAppVersion}"
Compression=lzma
SolidCompression=yes

[Languages]

; 默认安装包打包出来是英文版,并且没有中文视图,需要自己添加

Name: "Chinese"; MessagesFile: "compiler:Languages/Chinese.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone

[Files]
Source: "..\Doc\agreement_chs.txt"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\Doc\agreement_chs.txt"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\Doc\Readme.txt"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#ReleaseSourcePath}\eTMS.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#ReleaseSourcePath}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\Dependency\other\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\Dependency\reggridpp\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\Dependency\szb\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{commondesktop}\升级程序"; Filename: "{app}\UpgraderClient.exe"; Tasks: desktopicon
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\升级程序"; Filename: "{app}\UpgraderClient.exe"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
Filename: "{app}\{#RegGridPPBatName}"; Description: "{cm:LaunchProgram,{#StringChange(RegGridPPName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[Messages]  
BeveledLabel={#MyAppPublisher}
ConfirmUninstall="您真的想要从电脑中卸载{#MyAppName}吗?%n%n按【是】则完全删除 %1 以及它的所有组件;%n按 【否】则让软件继续留在您的电脑上. "

[Registry]  
Root: HKLM ;SubKey:"Software\{#MyAppName}";ValueType:dword;ValueName:config;ValueData:10 ;Flags:uninsdeletevalue  


;在执行脚本  
[code]  

//全局变量
var
ErrorCode,IsRunning: Integer;
const WM_CLOSE=$0010;


// 程序是否运行或在登录中
function GetLogedOrLoggingWindow() : Integer;
begin
  result:=FindWindowByWindowName('{#MyAppLoggedName}');
  if(result = 0) then
  begin
    result:=FindWindowByWindowName('{#MyAppLoggingName}');  
  end;
end;

// 检测.net framework 4.0安装环境,并安装.net框架
function CheckDotNetFrameWork() : Boolean;
var
ResultCode: Integer;
Path, dotNetV4RegPath, dotNetV4PackFile, wic : string;
begin
       dotNetV4RegPath:='SOFTWARE\Microsoft\.NETFramework\policy\v4.0';
    dotNetV4PackFile:='{src}\环境配置\dotNetFx40_Full_x86_x64.exe';
    wic:='{src}\环境配置\wic_x86_chs.exe';
    if RegKeyExists(HKLM, dotNetV4RegPath) then begin
        Result := true;
    end
    else begin
      // Exec(ExpandConstant(wic), '/q /norestart', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);  // 安装wic,windows xp系统会需要安装wic
        if MsgBox('正在安装客户端必备组件.Net Framework 4.0,可能会花费几分钟,请稍后……', mbConfirmation, MB_YESNO) = idYes then begin
            Path := ExpandConstant(dotNetV4PackFile);
            if(FileOrDirExists(Path)) then begin
                Exec(Path, '/norestart', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
                Result := true;
            end
            else begin                                                                                       
                if MsgBox('软件安装目录中没有.Net Framework的安装程序,' #13#13 '单击【是】跳过.Net Framework 4.0安装,【否】将退出安装!', mbConfirmation, MB_YESNO) = idYes then begin
                    Result := true;
                end
                else begin
                // path := expandconstant('{pf}\internet explorer\iexplore.exe'); //从官网下载
                // exec(path, 'http://www.microsoft.com/en-us/download/confirmation.aspx?id=17851', '', sw_shownormal, ewwaituntilterminated, resultcode);
                // msgbox('请安装好.net framework4.0环境后,再运行本安装包程序!',mbinformation,mb_ok);
                Result := false;
                end;
            end;
        end
        else begin
            Result := false;
        end;
    end;
end;

// 卸载之前的安装版本
function UnInstallBefore() : Integer;
var ResultCode : Integer;
UnInstallFile : String;
begin
    ResultCode:=-1;
    UnInstallFile:=''
    RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{A6A7578-84EC-4006-8226-C87D71FB94}_is1', 'UninstallString', UnInstallFile);
    StringChangeEx(UnInstallFile, '"', '', True);
    if(FileExists(UnInstallFile)) then begin
        Exec(UnInstallFile, '/norestart', ExtractFilePath(UnInstallFile), SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
    end;

    Result :=ResultCode;
end;
   
//判断程序是否存在  
//初始华程序事件   
function InitializeSetup() : Boolean;
begin  
    Result :=true; //安装程序继续  
    IsRunning:=GetLogedOrLoggingWindow()
    while IsRunning<>0 do
    begin  
        if Msgbox('系统检测到客户端正在运行,请确认是否关闭?' #13#13 '单击【是】自动关闭客户端并继续安装,【否】退出安装!', mbConfirmation, MB_YESNO) = idNO then begin  
            Result :=false; //安装程序退出  
            IsRunning :=0;
            exit;
        end
        else begin
            SendMessage(IsRunning,WM_CLOSE,0,0); // 关闭进程
            Result :=true; // 安装程序继续
            IsRunning:=GetLogedOrLoggingWindow()
        end;
    end;
    // 检查是否已经安装过应用程序
    if RegValueExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\{#MyAppName}', 'config') then begin
        if Msgbox('客户端已安装过,是否卸载重装?' #13#13 '单击【是】卸载并重装,【否】退出安装!', mbConfirmation, MB_YESNO) = idYES then begin
            UnInstallBefore();
            Result :=true; //安装程序继续  
        end  
        else begin
            Result :=false; //安装程序退出  
            IsRunning :=0;  
        end
    end
    else begin
        Result :=CheckDotNetFrameWork(); //安装程序继续  
        IsRunning:=GetLogedOrLoggingWindow();  
    end;
end;  

// 卸载时判断客户端是否正在运行  
function InitializeUninstall() : Boolean;  
begin  
    Result :=true; //安装程序继续
    IsRunning:=GetLogedOrLoggingWindow();  
    while IsRunning<>0 do  
    begin  
        if Msgbox('系统检测到客户端正在运行,请确认是否关闭?' #13#13 '单击【是】自动关闭客户端并继续卸载,【否】退出!', mbConfirmation, MB_YESNO) = idNO then begin  
            Result :=false; //安装程序退出  
            IsRunning :=0;  
        end
        else begin
            SendMessage(IsRunning,WM_CLOSE,0,0);
            Result :=true; //安装程序继续  
            IsRunning:=GetLogedOrLoggingWindow();  
        end;  
    end;  
end;  





参照文档:

http://blog.youkuaiyun.com/ahao1985507/article/details/39182977 安装包中的code部分参照

http://blog.youkuaiyun.com/goodadult2012/article/details/6442035 自动检测.net framework

http://www.cnblogs.com/liancs/archive/2012/09/14/2685061.html  安装视图汉化方法













### 使用 Inno Setup 安装 .NET 框架 为了确保应用程序能够在目标机器上正常运行,可以利用 Inno Setup 创建自定义安装程序,在此过程中检查并按需安装特定版本.NET Framework。 #### 准备工作 首先需要准备的是要分发的应用程序以及所需的 .NET Framework 版本离线安装包。对于 .NET Framework 2.0 的情况,应将其放置于易于访问的位置以便后续调用[^1]。 #### 编写 InstallScript 脚本 编写用于控制整个安装流程的脚本文件(.iss),其中包含了关于产品信息、源文件路径等基本信息设置外,还需加入针对 .NET Framework 存在性的判断逻辑: ```pascal ; 设置默认编码为 UTF-8 [Setup] DefaultDirName={pf}\MyApp OutputBaseFilename=setup_myapp_with_dotnetframework_checker Compression=lzma SolidCompression=yes [Languages] Name: "english"; MessagesFile: "compiler:Default.isl" [Tasks] Name: desktopicon; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; [Files] Source: "C:\Path\To\YourApplication.exe"; DestDir: "{app}"; Source: "dotNetFx40_Full_x86_x64.exe"; Flags: dontcopy; [RUN] Filename: {tmp}\dotNetFx40_Full_x86_x64.exe; StatusMsg: Installing Microsoft .NET Framework...; Check: not IsDotNetDetected('v4', '4.0.30319'); Parameters: "/q /norestart"; [Code] function InitializeSetup(): Boolean; begin Result := True; end; // 判断是否存在指定版本.NET Framework function IsDotNetDetected(version, sp: String): Boolean; var sValue: string; begin RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\.NETFramework\Policy\v' + version, 'InstallRoot', sValue); Result := (Pos(sp, sValue) > 0); end; ``` 上述代码片段展示了如何通过 Pascal Script 实现对 .NET Framework 是否存在的验证,并仅当缺少相应版本时才触发其安装过程[^3]。 #### 自动化处理机制 除了简单的存在性检测之外,还可以进一步增强自动化程度,比如根据操作系统位数选择合适的架构版本进行部署;或是预先设定好静默安装参数以减少用户交互次数等等[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值