请把轻狂志网址 www.flighty.cn 加入收藏夹,其他俱为假冒,谨防被骗!赞助请点 这里!找东东请用右边的搜索 ===>
01 2018-04

NSIS:判断并安装.NET Framework 4 的例子

作者:少轻狂 | 发布:2011-05-27 | 更新:2018-04-01 | 分类:部署 | Disposition | 热度:

现在.NET开发的软件已经大行其道,但有些操作系统并不符合软件的运行要求,所以,我们需要在安装程序中进行.NET Framework的判断,如果不符合要求就进行静默安装。

 

首先,我们需要加一个头文件进行版本比较之用:

!include "WordFunc.nsh"

然后加一个函数取得当前.NET版本号:
 

Function GetNetFrameworkVersion
	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\v4\Client" "Install"
	ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client" "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

最后,在Section区段进行比较版本号并静默安装.NET:

Section -.NET
	Call GetNetFrameworkVersion
	Pop $R1
	${VersionCompare} "4.5.50709" "$R1" $R2
	${If} $R2 == 1
 		SetDetailsPrint textonly
 		DetailPrint "正在安装 .NET Framework 4"
 		SetDetailsPrint listonly

 		SetOutPath "$TEMP"
 		SetOverwrite on
 		File "dotNetFx40_Full_x86_x64.exe"
 		ExecWait '"$TEMP\dotNetFx40_Full_x86_x64.exe" /q /norestart /ChainingPackage FullX64Bootstrapper'
 		Delete "$TEMP\dotNetFx40_Full_x86_x64.exe"
	${EndIf}
SectionEnd

当然,在Section区段可以做到自动在线下载而不把.NET打到安装包中,这样可以减小安装包体积,效果可参考紫辰记账本安装程序

附:比较版本号
Section - "比较版本号"
	Call GetNetFrameworkVersion
	Pop $R1
	${VersionCompare} "4.5.50709" "$R1" $R2
	${If} $R2 == 0
		MessageBox MB_OK "版本相同($R1)"
	${ElseIf} $R2 == 1
		MessageBox MB_OK "已安装版本低($R1)"
	${ElseIf} $R2 == 2
		MessageBox MB_OK "已安装版本高($R1)"
	${EndIf}
SectionEnd

 

标签:NSIS(49)NET(1)