如何在Windows注册表检测,如果用户已经安装了.NET Framework?注册表、用户、安装了、如何在

2023-09-04 13:38:50 作者:寄情书醉清风

我如何检测在Windows注册表中,如果用户已经安装了.NET Framework?我不是在寻找一个基于.Net的解决方案,因为查询是从InnoSetup编写。

How do I detect in the Windows Registry if a user has .Net Framework installed? I am not looking for a .Net based solution, as the query is from InnoSetup.

我知道,从这里看另一篇文章对堆栈溢出了.NET Framework是就地升级到4.0。

I know from reading another post here on Stack Overflow that .Net Framework is an inplace upgrade to 4.0.

我已经知道如何检查,如果用户拥有4.0版本的系统上安装的,即通过检查以下内容:

I already know how to check if a user has version 4.0 installed on the system, namely by checking the following:

function FindFramework(): Boolean; 
var
 bVer4x0: Boolean;
 bVer4x0Client: Boolean;
 bVer4x0Full: Boolean;
 bSuccess: Boolean;
 iInstalled: Cardinal;
begin
 Result := False;
 bVer4x0Client := False;
 bVer4x0Full := False;


 bVer4x0 := RegKeyExists(HKLM, 'SOFTWARE\Microsoft\.NETFramework\policy\v4.0'); 
 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4    \Client', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then bVer4x0Client := True;
 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4        \Full', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then bVer4x0Full := True;

 if (True = bVer4x0Full) then begin
    Result := True;
 end;
end;

我查了注册表,没有V4.5的文件夹,这是有道理的,如果.Net框架4.5是就地升级。不过,控制面板程序和功能,包括上市。

I checked the registry and there is no v4.5 folder, which makes sense if .Net Framework 4.5 is an inplace upgrade. Still, the Control Panel Programs and Features includes the listing.

我知道这可能是发行dotNetFx45_Full_setup.exe / Q不会有任何不良影响,如果安装已经有4.5版本的系统上,但我还是想不安装升级,如果升级已经存在,更快和更的问题。

I know that probably "issuing dotNetFx45_Full_setup.exe /q" will have no bad effect if installing on a system that already has version 4.5, but I still would like to not install the upgrade if the upgrade already exists, faster and less problems.

推荐答案

我想和大家分享实际的创新安装code,我写的,具体回答了我的问题。这要归功于推我的方向是正确的previous答案。

I wanted to share the actual Inno Setup code that I wrote, which specifically answers my question. Thanks to the previous answer for pushing me in the right direction.

function FindFramework45(): Boolean; 
var
 bVer4x5: Boolean;
 bSuccess: Boolean;
 iInstalled: Cardinal;
 strVersion: String;
 iPos: Cardinal;
begin
 Result := False;

 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then
  begin
    bSuccess := RegQueryStringValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Version', strVersion);
    if (True = bSuccess) then
     Begin
        iPos := Pos('4.5.', strVersion);
        if (0 < iPos) then bVer4x5 := True;
     End
  end;

 if (True = bVer4x5) then begin
    Result := True;
 end;
end;

在NextButtonClick()事件会欢迎之后调用此函数。

The NextButtonClick() event would call this function right after the welcome.

文件并运行区段只包含一个函数,它使用了检查,并检查该变量。

The File and Run sections merely contain a function which uses the Check and checks that variable.

[Run]
Filename: "{app}\dotNetFx45_Full_setup.exe"; Parameters: "/q"; StatusMsg: "Installing Microsoft .Net 4.5 Framework..."; Check: InstallFramework45();

[Files]
Source: "{#MySourceBaseDir}\{#MyAppVersion}\{#MyDirBinaries}\dotNetFx45_Full_setup.exe"; Flags: deleteafterinstall; DestDir: "{app}"; Check: InstallFramework45();

我会陈述明显,人们必须确保了.NET Framework 4.0的安装,然后再检查/安装.NET Framework 4.5。

I will state the obvious that one has to make sure that .Net Framework 4.0 is installed first and then check / install .Net Framework 4.5.

现在,如果Sy​​stem.Data.SQLite.org会拿出一个Visual Studio 2012兼容的版本,我可以检查了我的另一大升级任务。

Now, if System.Data.SQLite.org would come out with a Visual Studio 2012 compliant version, I can check off my other big upgrade task.

更新:2010.10.11(每TLana的评论) 注:我决定离开原来的code,因为我想别人想看到我的开始。在code以下就是我在现在。下面还有code使用为.NET 4.0中的新的.NET 4.5正确的注册表位置和检查。怎么样的未来?当净6.0和6.5出来,所有需要的是改变4到6,除非微软改变了配方。似乎.5升级是不是一个新的框架,但升级到现有的。

Update: 2010.10.11 (Per TLana's comment) Note: I decided to leave the original code, because I figure that others would like to see where I started. The code below is where I am at now. The code below also uses the proper registry location and checks for both .Net 4.0 and the new .Net 4.5. What about the future? When .Net 6.0 and 6.5 comes out, all that is needed is change the 4 to a 6, unless Microsoft changes the formula. it seems that the .5 upgrade is not a new framework but an upgrade to the existing one.

[Files]
Source: "{#MySourceBaseDir}\{#MyDirBinaries}\dotNetFx40_Full_x86_x64.exe"; Flags: deleteafterinstall; DestDir: "{app}"; Check: Framework40IsNotInstalled();
Source: "{#MySourceBaseDir}\{#MyDirBinaries}\dotNetFx45_Full_setup.exe"; Flags: deleteafterinstall; DestDir: "{app}"; Check: Framework45IsNotInstalled();
Source: "{#MySourceBaseDir}\{#MyDirBinaries}\mysql-connector-net-6.5.4.msi"; Flags: deleteafterinstall; DestDir: "{tmp}";

[Run]
Filename: "{app}\vcredist_x86.exe"; Parameters: "/q"; StatusMsg: "Installing Microsoft Visual C++ 2010 Redistributable Package...";
Filename: "{app}\dotNetFx40_Full_x86_x64.exe"; Parameters: "/q"; StatusMsg: "Installing Microsoft .Net 4.0 Full Framework..."; Check: Framework40IsNotInstalled();
Filename: "{app}\dotNetFx45_Full_setup.exe"; Parameters: "/q"; StatusMsg: "Installing Microsoft .Net 4.5 Framework..."; Check: Framework45IsNotInstalled();
Filename: "msiexec"; Parameters: "/package ""{tmp}\mysql-connector-net-6.5.4.msi"" /quiet"; StatusMsg: "Installing MySQL Connector...";

[Code]

function Framework40IsNotInstalled: Boolean;
var
 bVer4x0Client: Boolean;
 bVer4x0Full: Boolean;
 bSuccess: Boolean;
 iInstalled: Cardinal;
begin
 Result := True;
 bVer4x0Client := False;
 bVer4x0Full := False;


 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Client', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then bVer4x0Client := True;
 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then bVer4x0Full := True;

 if (True = bVer4x0Full) then begin
    Result := False;
 end;
end;

function Framework45IsNotInstalled: Boolean;
var
 bVer4x5: Boolean;
 bSuccess: Boolean;
 iInstalled: Cardinal;
 strVersion: String;
 iPos: Cardinal;
begin
 Result := True;
 bVer4x5 := False;

 bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Install', iInstalled);
 if (1 = iInstalled) AND (True = bSuccess) then
  begin
    bSuccess := RegQueryStringValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Version', strVersion);
    if (True = bSuccess) then
     Begin
        iPos := Pos('4.5.', strVersion);
        if (0 < iPos) then bVer4x5 := True;
     End
  end;

 if (True = bVer4x5) then begin
    Result := False;
 end;
end;