Content:
|
Sometimes, you need to install something on a machine, and you need to know if the .NET Framework (and which version(s)) is installed on that machine. It's easy if you're behind the keyboard, but that's not always the case. For that purpose, I needed some code to check to see if the .NET Framework is installed.
http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;315291 tells me that this can be done by looking at the registry, which results in the following Delphi code:
program DetectDotNet; {$APPTYPE CONSOLE} uses Windows, Registry;
type DotNetVersion = (v10, v11);
function FrameworkInstalled(version: DotNetVersion): Boolean; const KeyNames: array[DotNetVersion] of String = ('v1.0', 'v1.1'); Value: array[DotNetVersion] of String = ('3705', '4322'); var Reg: TRegistry; begin Result := False; Reg := TRegistry.Create(KEY_READ); try Reg.RootKey := HKEY_LOCAL_MACHINE; if Reg.OpenKey('SOFTWARE\Microsoft\.NETFramework\policy', False) then if Reg.OpenKey(KeyNames[version], False) then Result := Pos(Value[version],Reg.ReadString(Value[version])) > 0 finally Reg.Free end end;
begin if FrameworkInstalled(v10) then writeln('.NET Framework 1.0 installed'); if FrameworkInstalled(v11) then writeln('.NET Framework 1.1 installed'); readln end.
The downside of this method is that I need to manually update the code once the final version of .NET 2.0 is available. Right now, the beta of .NET 2.0 that I have can be detected with version 2.0.50215, while I’ve also seen version 2.0.40607 for an earlier beta. Having to modify my detection routine for a moving targer doesn't feel good.
A more flexible solution might be not to use hard coded registry keys, but instead look for the mscorlib.dll in the subdirectories of the %windir%\Microsoft.NET directory. The actual location of that Microsoft.NET directory must first be obtained from the registry, from the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework key, this time looking at the value of the InstallRoot key. Once we have the install root, we can look for subdirectories. For each subdirectory, we should try to load the mscorlib.dll, but specifically in that subdirectory. If the mscorlib.dll can be loaded, then that subdirectory contains a valid version of the .NET Framework, with the additional benefit that we immediately know the version number, since that’s the name of the subdirectory. All this is implemented with the following little Delphi console application.
program DotNetVersion; {$APPTYPE CONSOLE} uses Windows, SysUtils, Registry;
procedure GetVersion(const SubDir: String); const DotNet = 'mscorlib.dll'; var HMod: HModule; begin HMod := LoadLibrary(PChar(SubDir + '\' + DotNet)); if HMod >= 32 then begin writeln('.NET Framework ',SubDir,' installed'); FreeLibrary(HMod) end end;
var Reg: TRegistry; Root: String; SRec: TSearchRec; begin Reg := TRegistry.Create(KEY_READ); try Reg.RootKey := HKEY_LOCAL_MACHINE; if Reg.OpenKey('SOFTWARE\Microsoft\.NETFramework', False) then Root := Reg.ReadString('InstallRoot') finally Reg.Free end; //writeln(Root); ChDir(Root); if FindFirst('*.*', faDirectory, SRec) = 0 then repeat if (SRec.Attr and faDirectory) = faDirectory then if (SRec.Name <> '.') and (SRec.Name <> '..') then GetVersion(SRec.Name) until FindNext(SRec) <> 0; readln end.
This should report any (beta) version of the .NET Framework (until Microsoft decides to change the rules again, of course).
|