Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Drs.Bob) Dr.Bob's Delphi Clinics Dr.Bob's Delphi Courseware Manuals
View Bob Swart's profile on LinkedIn Drs.Bob's Delphi Notes
These are the voyages using Delphi Enterprise (and Architect). Its mission: to explore strange, new worlds. To design and build new applications. To boldly go...
Title:

Detecting the .NET Framework versions

Author: Bob Swart
Posted: 8/23/2005 10:03:12 AM (GMT+1)
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\Microsof­t\.NETFramework\policy', False) then
if Reg.OpenKey(KeyNames[version], False) then
Result := Pos(Value[version],Reg.ReadStr­ing(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).

Back  


5 Comments

AuthorPostedComments
Marc Scheuner05/08/24 07:46:02The <$APPTYPE CONSOLE> is causing problems - compile errors, to be exact. I guess you should have written {$APPTYPE CONSOLE} (curly braces) instead, no?
Bob Swart05/08/24 08:32:57Yes sorry, I fixed the source code. Thanks.
Wyatt Wong05/11/25 09:27:07Under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework registry folder, there is sdkInstallRoot[version] sub-keys which indiciate the .NET Framework SDK installed. It would easier to search the string with prefix of 'sdkInstallRoot' and find out which version of .NET Framework SDK is/are installed. However, I believed the above methods mean to retrieve the version of .NET Framework ***RUNTIME*** Note that to find the version of .NET Framework SDK and .NET Framework Runtime are 2 different things.
Bob Swart05/12/09 14:46:32Thanks for the tip for the .NET SDK, Wyatt.
Jeffrey Giles 07/11/25 15:02:36This is just what I need to determine if and which version of .NET needs to be installed to install Delphi 2007. Thanks


New Comment (max. 2048 characters, no HTML):

Name:
Comment:



This webpage © 2005-2019 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.