如何使用Delphi 7检测.net框架版本

时间:2010-04-19 04:04:52

标签: .net delphi

如何使用Delphi 7检测.net框架版本?

3 个答案:

答案 0 :(得分:5)

您可以检查Windows注册表以检测已安装的网络框架的版本。

请参阅此代码

program DetectNetFrameWork;

{$APPTYPE CONSOLE}

uses
  Windows,
  Registry,
  SysUtils;

const
NetFrameworkv1   ='SOFTWARE\Microsoft\.NETFramework\policy\v1.0';
NetFrameworkv1_1 ='SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322';
NetFrameworkv2   ='SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727';
NetFrameworkv3   ='SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0';
NetFrameworkv3_5 ='SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5';
NetFrameworkv4   ='SOFTWARE\Microsoft\NET Framework Setup\NDP\v4';


function NetFrameworkInstalled(NetFrameWorkId:String): Boolean;
var
Reg: TRegistry;
begin
Result := False;
  Reg := TRegistry.Create(KEY_READ);
  try
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  Result := Reg.KeyExists(NetFrameWorkId);
  finally
  Reg.Free;
  end
end;

function BoolToStr(Val : Boolean): String;
begin
if val  then
result := 'True'
else
result := 'False';
end;



begin
  Writeln('Net Framework 1.0 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv1)));
  Writeln('Net Framework 1.1 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv1_1)));
  Writeln('Net Framework 2.0 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv2)));
  Writeln('Net Framework 3.0 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv3)));
  Writeln('Net Framework 3.5 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv3_5)));
  Writeln('Net Framework 4.0 Installed = '+BoolToStr(NetFrameworkInstalled(NetFrameworkv4)));
  Readln;
end.

您可以查看这些链接以获取更多信息

答案 1 :(得分:2)

答案 2 :(得分:0)

将@RRUZ的答案扩展为4.5& 4.6, GetDOTNETVersion 返回最高安装的.NET版本。

function GetDOTNETVersion: string;
    function NetFrameworkInstalled(NetFrameWorkId: String): string;
    begin
        Result := '0';
        with TRegistry.Create(KEY_READ) do
            try
                RootKey := HKEY_LOCAL_MACHINE;
                if KeyExists(NetFrameWorkId) then
                try
                    OpenKeyReadOnly(NetFrameWorkId);
                    Result := ReadString('Version');
                    if Result.IsEmpty then  // v4 & v4.0
                    begin
                        CloseKey;
                        OpenKeyReadOnly(NetFrameWorkId+'\Client');
                        Result := ReadString('Version');
                    end;
                finally
                    CloseKey;
                end;
            finally
                Free;
            end;
    end;

const
    NetFrameworkv1 = 'SOFTWARE\Microsoft\.NETFramework\policy\v1.0';
    NetFrameworkv1_1 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322';
    NetFrameworkv2 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727';
    NetFrameworkv3 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0';
    NetFrameworkv3_5 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5';
    NetFrameworkv4_0 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4.0';
    NetFrameworkv4 = 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4';
begin
    Result := NetFrameworkInstalled(NetFrameworkv4);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv4_0);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv3_5);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv3);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv2);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv1_1);
    if Result.IsEmpty then Result := NetFrameworkInstalled(NetFrameworkv1);
end;