Get directory of installed .NET runtime

From NSIS Wiki
Jump to navigationJump to search

Description

Given a .Net version number, this function returns that .Net framework's install directory. This is useful if you intend to call 'RegAsm.exe', or 'GACUtil.exe' on one of your .Net assembly DLLs, since these executables are stored in the directory this function returns.

Parameters

in: version of .Net framework required (eg. "v2.0")
out: directory in which it is installed (eg. "C:\WINNT\Microsoft.NET\Framework\v2.0.50727")

Returns "" if the specified version of the .Net framework does not appear to be installed.

Usage

Section "Install"
 
	; get directory of .NET framework installation
	Push "v2.0"
	Call GetDotNetDir
	Pop $R0 ; .net framework v2.0 installation directory
	StrCmpS "" $R0 err_dot_net_not_found
 
	; Perform our install
	; e.g. use the .Net path in $R0 to call RegAsm.exe
	SetOutPath $INSTDIR
	File "MyDll.dll"
	ExecWait '"$R0\RegAsm.exe" MyDll.dll'
 
	Return
 
err_dot_net_not_found:
	Abort "Aborted: .Net framework not found."
 
SectionEnd

Function

; Given a .NET version number, this function returns that .NET framework's
; install directory. Returns "" if the given .NET version is not installed.
; Params: [version] (eg. "v2.0")
; Return: [dir] (eg. "C:\WINNT\Microsoft.NET\Framework\v2.0.50727")
Function GetDotNetDir
	Exch $R0 ; Set R0 to .net version major
	Push $R1
	Push $R2
 
	; set R1 to minor version number of the installed .NET runtime
	EnumRegValue $R1 HKLM \
		"Software\Microsoft\.NetFramework\policy\$R0" 0
	IfErrors getdotnetdir_err
 
	; set R2 to .NET install dir root
	ReadRegStr $R2 HKLM \
		"Software\Microsoft\.NetFramework" "InstallRoot"
	IfErrors getdotnetdir_err
 
	; set R0 to the .NET install dir full
	StrCpy $R0 "$R2$R0.$R1"
 
getdotnetdir_end:
	Pop $R2
	Pop $R1
	Exch $R0 ; return .net install dir full
	Return
 
getdotnetdir_err:
	StrCpy $R0 ""
	Goto getdotnetdir_end
 
FunctionEnd

Issues

It is difficult to know for sure whether calls to external programs, such as RegAsm.exe, in the example above, really succeeded. Redirecting the output of RegAsm.exe to a file, and then opening the file to look for the magic words "Types succesfully registered." will yeild some success, but is problematic on older version of Windows (NT or 95/98/ME).

More reliably, and more simply, use 'nsExec', an extension which ships with the default NSIS install, to call external executables like RegAsm.exe. This automatically displays the stdout from the called executable in the install details log, and also checks for successfull execution.