InnoSetup编写:验证.NET 4.0安装InnoSetup、NET

2023-09-02 21:09:56 作者:掌中花.

我有需要.NET 4.0运行一个组成部分,如何让我的Inno Setup安装程序验证它是否已安装,如果没有,提示用户安装?

I have a component that requires .NET 4.0 to run, how can my Inno Setup installer verify that it is installed, and if not, prompt the user to install it?

推荐答案

在创新安装可执行文件运行时,InitializeSet​​up函数被调用。插入此code为自定义脚本应该做你想要的:

The InitializeSetup function is called when the Inno Setup executable is run. Inserting this code for a custom script should do what you want:

function IsDotNetDetected(version: string; service: cardinal): boolean;
// Indicates whether the specified version and service pack of the .NET Framework is installed.
//
// version -- Specify one of these strings for the required .NET Framework version:
//    'v1.1.4322'     .NET Framework 1.1
//    'v2.0.50727'    .NET Framework 2.0
//    'v3.0'          .NET Framework 3.0
//    'v3.5'          .NET Framework 3.5
//    'v4Client'     .NET Framework 4.0 Client Profile
//    'v4Full'       .NET Framework 4.0 Full Installation
//
// service -- Specify any non-negative integer for the required service pack level:
//    0               No service packs required
//    1, 2, etc.      Service pack 1, 2, etc. required
var
    key: string;
    install, serviceCount: cardinal;
    success: boolean;
begin
    key := 'SOFTWAREMicrosoftNET Framework SetupNDP' + version;
    // .NET 3.0 uses value InstallSuccess in subkey Setup
    if Pos('v3.0', version) = 1 then begin
        success := RegQueryDWordValue(HKLM, key + 'Setup', 'InstallSuccess', install);
    end else begin
        success := RegQueryDWordValue(HKLM, key, 'Install', install);
    end;
    // .NET 4.0 uses value Servicing instead of SP
    if Pos('v4', version) = 1 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
    end else begin
        success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
    end;
    result := success and (install = 1) and (serviceCount >= service);
end;

function InitializeSetup(): Boolean;
begin
    if not IsDotNetDetected('v4Client', 0) then begin
        MsgBox('MyApp requires Microsoft .NET Framework 4.0 Client Profile.'#13#13
            'Please use Windows Update to install this version,'#13
            'and then re-run the MyApp setup program.', mbInformation, MB_OK);
        result := false;
    end else
        result := true;
end;

(code取自这里:http://www.kynosarges.de/DotNetVersion.html)

首先,它检查注册表项指示安装的.NET Framework版本的presence。如果该注册表项不是present,它会提示下载.NET框架的用户。如果用户说是的,它会打开下载URL。 (您可能需要更改指定脚本4.0版本的版本。)

First, it checks for the presence of a registry entry that indicates the version of the .NET framework that is installed. If the registry entry is not present, it prompts the user to download the .NET framework. If the user says Yes, it opens the download URL. (You may have to change the version it specifies in the script to version 4.0.)

我还隔着来到这篇文章$ C $的CProject ,这可能是一个更COM prehensive并做你正在寻找的东西,虽然它可能需要更多的工作来了解,将不得不进行修改,与4.0版本一起工作的定制方式。

I also came across this article on CodeProject, which may be a more comprehensive and customizable way of doing what you're looking for, although it may take more work to understand and will have to be modified to work with version 4.0.