最近在用NSIS在安装过程中需要检测是否安装.net Framework ,这里的代码将自动通过检测注册表内的相关数值来实现,如果系统没有安装则进行在线下载并静默安装,相关的参数都在下文代码中,加入NSIS脚本中即可。相关的下载地址可替换字自己的或者其他地址,以避免失效。

Section -.NET Framework
  ;检测是否是需要的.NET Framework版本
  Call GetNetFrameworkVersion
  Pop $R1
  ;${If} $R1 < '2.0.50727'
  ;${If} $R1 < '3.5.30729.4926'
  ${If} $R1 < '4.0.30319'
  ;${If} $R1 < '4.5.52747'
    MessageBox MB_YESNO|MB_ICONQUESTION "
此软件运行需要.NET Framework 4.0运行环境,但您机器上似乎没有安装此环境。$\r$\n您有两个选择:$\r$\n1.您自己到互联
网上下载.NET Framework 4.0安装,然后再运行此软件$\r$\n2.使用此安装程序在线下载并安
装.NET Framework 4.0$\r$\n$\r$\n现在在线下载并安装.NET Framework 4.0,请确认您的机器已连接互联
网.继续吗?" IDNO +2
      Call DownloadNetFramework4
    ${ENDIF}
SectionEnd
  
Function GetNetFrameworkVersion
;获取.Net Framework版本支持
    Push $1
    Push $0
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "2.0.50727.832"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "1.1.4322.573"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"
    ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"
    StrCmp $1 "" +1 +2
    StrCpy $1 "1.0.3705.0"
    StrCmp $0 1 KnowNetFrameworkVersion +1
    StrCpy $1 "not .NetFramework"
    KnowNetFrameworkVersion:
    Pop $0
    Exch $1
FunctionEnd
  
Function DownloadNetFramework2
;下载 .NET Framework 2.0 SP2
  NSISdl::download /TRANSLATE2 '正在下载 %s' '正在连接...' '(剩余 1 秒)' '(剩余 1 分钟)' '(剩余 1 小时)' '(剩余 %u 秒)' '(剩余 %u 分钟)' '(剩余 %u 小时)' '已完成:%skB(%d%%) 大小:%skB 速度:%u.%01ukB/s' /TIMEOUT=7500 /NOIEPROXY 'http://download.microsoft.com/download/c/6/e/c6e88215-0178-4c6c-b5f3-158ff77b1f38/NetFx20SP2_x86.exe' '$TEMP\NetFx20SP2_x86.exe'
  Pop $R0
  StrCmp $R0 "success" 0 +3
  
  SetDetailsPrint textonly
  DetailPrint "正在安装 .NET Framework 2.0 SP2..."
  SetDetailsPrint listonly
  ExecWait '$TEMP\NetFx20SP2_x86.exe /quiet /norestart' $R1
  Delete "$TEMP\NetFx20SP2_x86.exe"
  
FunctionEnd
  
Function DownloadNetFramework35
;下载 .NET Framework 3.5 SP1
  NSISdl::download /TRANSLATE2 '正在下载 %s' '正在连接...' '(剩余 1 秒)' '(剩余 1 分钟)' '(剩余 1 小时)' '(剩余 %u 秒)' '(剩余 %u 分钟)' '(剩余 %u 小时)' '已完成:%skB(%d%%) 大小:%skB 速度:%u.%01ukB/s' /TIMEOUT=7500 /NOIEPROXY 'http://download.microsoft.com/download/2/0/E/20E90413-712F-438C-988E-FDAA79A8AC3D/dotnetfx35.exe' '$TEMP\dotnetfx35.exe'
  Pop $R0
  StrCmp $R0 "success" 0 +2
  
  SetDetailsPrint textonly
  DetailPrint "正在安装 .NET Framework 3.5 SP1..."
  SetDetailsPrint listonly
  ExecWait '$TEMP\dotnetfx35.exe /quiet /norestart' $R1
  Delete "$TEMP\dotnetfx35.exe"
  
FunctionEnd
  
Function DownloadNetFramework4
;下载 .NET Framework 4.0
  NSISdl::download /TRANSLATE2 '正在下载 %s' '正在连接...' '(剩余 1 秒)' '(剩余 1 分钟)' '(剩余 1 小时)' '(剩余 %u 秒)' '(剩余 %u 分钟)' '(剩余 %u 小时)' '已完成:%skB(%d%%) 大小:%skB 速度:%u.%01ukB/s' /TIMEOUT=7500 /NOIEPROXY 'http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe' '$TEMP\dotNetFx40_Full_x86_x64.exe'
  Pop $R0
  StrCmp $R0 "success" 0 +2
    
  SetDetailsPrint textonly
  DetailPrint "正在安装 .NET Framework 4.0 Full..."
  SetDetailsPrint listonly
  ExecWait '$TEMP\dotNetFx40_Full_x86_x64.exe /quiet /norestart' $R1
  Delete "$TEMP\dotNetFx40_Full_x86_x64.exe"
    
FunctionEnd
  
Function DownloadNetFramework45
;下载 .NET Framework 4.5
  NSISdl::download /TRANSLATE2 '正在下载 %s' '正在连接...' '(剩余 1 秒)' '(剩余 1 分钟)' '(剩余 1 小时)' '(剩余 %u 秒)' '(剩余 %u 分钟)' '(剩余 %u 小时)' '已完成:%skB(%d%%) 大小:%skB 速度:%u.%01ukB/s' /TIMEOUT=7500 /NOIEPROXY 'http://download.microsoft.com/download/E/2/1/E21644B5-2DF2-47C2-91BD-63C560427900/NDP452-KB2901907-x86-x64-AllOS-ENU.exe' '$TEMP\NDP452-KB2901907-x86-x64-AllOS-ENU.exe'
  Pop $R0
  StrCmp $R0 "success" 0 +2
  
  SetDetailsPrint textonly
  DetailPrint "正在安装 .NET Framework 4.5.2 ..."
  SetDetailsPrint listonly
  ExecWait '$TEMP\NDP452-KB2901907-x86-x64-AllOS-ENU.exe /quiet /norestart' $R1
  Delete "$TEMP\NDP452-KB2901907-x86-x64-AllOS-ENU.exe"
  
FunctionEnd

文章链接: http://www.gongzi.org/nsis-check-net-framework-and-install.html  [复制]
站方声明: 除特别标注, 本站所有文章均为原创, 互联分享, 尊重版权, 转载请注明.