c# - 我使用 inno setup 来检测是否安装了 .net 4.0 客户端,但效果不佳

标签 c#

当我安装程序时,它会检查是否安装了 .net 4.0 客户端。

没有,那么应该安装它。

问题是,如果我再次运行安装程序,安装文件将尝试再次安装 .Net 4.0 客户端,但这一次它为我提供了修复或删除 .Net 4.0 客户端的选项。

我不知道为什么在后续尝试中它会再次尝试安装.Net。

另一个问题是,在 [运行] 部分中,我正在运行 ffdshow 以静默模式安装它,如何检查它是否已安装,以免再次安装?

这是我的脚本:

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

#define MyAppName "Lightnings Extractor"
#define MyAppVersion "Lightnings Extractor 1.0"
#define MyAppExeName "Lightnings Extractor.exe"
#define FfdshowExeName "

[_ISTool]
EnableISX=true


[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={{E60E9193-B054-4026-98EA-5DAD45CE9B0B}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputDir=D:\Lightnings_Extractor Setup
OutputBaseFilename=LE_Setup
SetupIconFile=D:\MyWeatherStation-Images-And-Icons\Weather_Michmoret.ico
Compression=lzma
SolidCompression=yes
PrivilegesRequired=admin

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

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

[Files]
Source: D:\Lightnings_Extractor Setup\Lightnings Extractor InnoSetup Script\isxdl\isxdl.dll; Flags: dontcopy
Source: "D:\C-Sharp\Extracting_Frames\Extracting_Frames\Extracting_Frames\bin\Release\Lightnings Extractor.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "D:\C-Sharp\Extracting_Frames\Extracting_Frames\Extracting_Frames\bin\Release\DirectShowLib-2005.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "D:\C-Sharp\Extracting_Frames\Extracting_Frames\Extracting_Frames\bin\Release\unfreez_wrapper.dll"; DestDir: "{app}"; Flags: ignoreversion
Source:  "D:\Appz\ffdshow_rev4225_20120105_clsid.exe"; DestDir: "{app}"; Flags: ignoreversion 
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Messages]
WinVersionTooLowError=MyApp requires Windows NT4, Windows 98 or later.

[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
Filename: {app}\ffdshow_rev4225_20120105_clsid.exe; Parameters: /silent; StatusMsg: Installing ffdshow...

[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=5765d7a8-7722-4888-a970-ac39b33fd8ab&srcdisplaylang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f7%2fB%2f6%2f7B629E05-399A-4A92-B5BC-484C74B5124B%2fdotNetFx40_Client_setup.exe';
  // local system for testing...    
  // dotnetRedistURL = 'http://192.168.1.1/dotnetfx.exe';

function InitializeSetup(): Boolean;

begin
  Result := true;
  dotNetNeeded := false;

  // Check for required netfx on windows xp installation
  if (not RegKeyExists(HKLM, 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0')) then begin
    dotNetNeeded := true;
    if (not IsAdminLoggedOn()) then begin
      MsgBox('MyApp 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}\dotnetfx.exe');
      if not FileExists(dotnetRedistPath) then begin
        dotnetRedistPath := ExpandConstant('{tmp}\dotnetfx.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

    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', 'Downloading Microsoft .NET Framework');
      isxdl_SetOption('description', 'MyApp needs to install the Microsoft .NET Framework. Please wait while Setup is downloading extra files to your computer.');
      if isxdl_DownloadFiles(hWnd) = 0 then Result := false;
    end;
    if (Result = true) and (dotNetNeeded = true) then begin
      if Exec(ExpandConstant(dotnetRedistPath), '', '', 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;

function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
  s: string;

begin
  if memoDependenciesNeeded <> '' then s := s + 'Dependencies to install:' + NewLine + memoDependenciesNeeded + NewLine;
  s := s + MemoDirInfo + NewLine + NewLine;

  Result := s
end;

//testing

function InitializeSetups(): Boolean;

begin
  Result := true;
  dotNetNeeded := false;

  // Check for required netfx on windows xp installation
  if (not RegKeyExists(HKLM, 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4')) then begin
    dotNetNeeded := true;
    if (not IsAdminLoggedOn()) then begin
      MsgBox('MyApp 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}\dotnetfx.exe');
      if not FileExists(dotnetRedistPath) then begin
        dotnetRedistPath := ExpandConstant('{tmp}\dotnetfx.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 NextButtonClicks(CurPage: Integer): Boolean;
var
  hWnd: Integer;
  ResultCode: Integer;

begin
  Result := true;

  if CurPage = wpReady 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', 'Downloading Microsoft .NET Framework');
      isxdl_SetOption('description', 'MyApp needs to install the Microsoft .NET Framework. Please wait while Setup is downloading extra files to your computer.');
      if isxdl_DownloadFiles(hWnd) = 0 then Result := false;
    end;
    if (Result = true) and (dotNetNeeded = true) then begin
      if Exec(ExpandConstant(dotnetRedistPath), '', '', 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;

function UpdateReadyMemos(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
  s: string;

begin
  if memoDependenciesNeeded <> '' then s := s + 'Dependencies to install:' + NewLine + memoDependenciesNeeded + NewLine;
  s := s + MemoDirInfo + NewLine + NewLine;

  Result := s
end;

最佳答案

您需要做的是在 Inno 中运行代码脚本并在注册表中查找 .Net 4:

[Files]
;Redistributables
Source: Redistributables\*; DestDir: {tmp}; Flags: ignoreversion deleteafterinstall


[Code]
var dotNET40Missing: Boolean; // Is the .NET 4.0 Framework missing entirely?

function InitializeSetup(): Boolean;
begin
    // Test the presence of .NET 4.0
    if (not(RegKeyExists(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4'))) then
        dotNET40Missing := True;

    Result := True;
end;

function ShouldInstalldotNET40(): Boolean;
begin
    Result := dotNET40Missing;
end;

[Run]
Filename: {tmp}\.NET 4.0.exe; Description: Install Microsoft .Net Framework 4.0;    Parameters: /q /noreboot; Flags: skipifdoesntexist; Check: ShouldInstalldotNET40

[文件] 将 .Net WebInstaller 复制到临时目录。 [运行] 如果 [代码] 返回 true,则安装 .Net 4

编辑

我注意到你有

if (not RegKeyExists(HKLM, 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0')

而不是

if (not RegKeyExists(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0')

关于c# - 我使用 inno setup 来检测是否安装了 .net 4.0 客户端,但效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9008905/

相关文章:

c# - 为 Oracle 返回一个记录集作为 HTML

c# - 通过跟踪监听器动态更改跟踪级别

c# - Json 反序列化 protected 属性

c# - 获取编译时错误 CS0579 : Duplicate 'AssemblyFileVersionAttribute' attribute

c# - 如何动态决定使用哪个静态类

c# - 如何将字符串转换为具有特定位数的十进制

c# - 协程函数在 WaitForSeconds 处停止

c# - 如何处理 Windows 应用程序中的 SecondaryTile 单击事件

c# - 在 Windows 窗体的属性网格中正确显示自定义对象列表

c# - 失去连接后MongoDB从当前位置继续