It can be difficult to detect which versions of the Microsoft .Net framework are installed and available on a Windows device. This article presents a method to detect information about installed Microsoft .Net Framework versions.
In addition to describing the method this article provides a functional class to query and detect the installed CLR versions as well as a command line tool that will print out .Net CLR information.
Method
There is no simple function call in the .Net framework to detect versions of installed frameworks. This does make sense though, because in the case that .Net is not available it would be impossible to execute such a function call. C++ native code solutions do exist to perform this function and one such implementation can be found on Aaron Stebner’s MSDN Blog here.
The method used in the tool provided in this article is to enumerate the installed frameworks in the %systemroot%\Microsoft.NET\Framework folder, open the mscorlib.dll files, and retrieve version build information. These version numbers can then be looked up in a table of descriptions to provide information about the service pack level the .Net version is.
Command Line Tool
A command line tool to query the installed Microsoft .Net frameworks can be downloaded here. This Lowe*Software tool was written using the C# snippet below.
Note that this tool is compiled under Microsoft .Net Framework 1.1 and requires a version of .Net to run.
C# Code
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace DotNetVersionInfo
{
/// <summary>
/// This class is used to retrieve information about the
/// Microsoft .Net Frameworks installed.
/// </summary>
public sealed class DotNetVersion
{
private DotNetVersion()
{
}
/// <summary>
/// Retrieves the version of the CLR version the process is
/// currently executing in.
/// </summary>
/// <returns>A string representing the CLR version.</returns>
public static string GetCurrentCLRVersion()
{
return System.Environment.Version.ToString();
}
/// <summary>
/// Retrieves a description of a CLR based on version. This
/// method will return information about service pack levels.
/// </summary>
/// <param name="version">A string representing the CLR version.</param>
/// <returns>A description of the CLR.</returns>
public static string GetCLRVersionDescription(string version)
{
switch(version)
{
case "1.0.3705.0":
return ".NET Framework 1.0 Original Release";
case "1.0.3705.209":
return ".NET Framework 1.0 Service Pack 1";
case "1.0.3705.288":
return ".NET Framework 1.0 Service Pack 2";
case "1.0.3705.6018":
return ".NET Framework 1.0 Service Pack 3";
case "1.1.4322.573":
return ".NET Framework 1.1 Original Release";
case "1.1.4322.2032":
return ".NET Framework 1.1 Service Pack 1";
case "1.1.4322.2300":
return ".NET Framework 1.1 Service Pack 1 (Windows Server 2003 SP1)";
case "2.0.40607.16":
return ".NET Framework 2.0 Beta 1";
case "2.0.50215.44":
return ".NET Framework 2.0 Beta 2";
case "2.0.50727.42":
return ".NET Framework 2.0 Original Release";
default:
return "Unknown Version";
}
}
/// <summary>
/// Retrieves a list of versions installed on the current machine.
/// </summary>
/// <returns>A string array of installed CLR versions.</returns>
public static string[] GetInstalledCLRVersions()
{
ArrayList versions=new ArrayList();
string frameworkpath=Path.Combine(
Environment.SystemDirectory,
;@"..Microsoft.NETFramework");
string[] dirs=Directory.GetDirectories(frameworkpath, "v*.*.*");
foreach(string dir in dirs)
{
try
{
if(Regex.IsMatch(dir, "v[0-9]{1,5}.[0-9]{1,5}.[0-9]{1,5}.[0-9]{1,5}"))
{
FileVersionInfo verinfo=FileVersionInfo.GetVersionInfo(
Path.Combine(frameworkpath, dir + ;@"Mscorlib.dll"));
versions.Add(
verinfo.FileMajorPart + "." +
verinfo.FileMinorPart + "." +
verinfo.FileBuildPart + "." +
verinfo.FilePrivatePart);
}
}
catch {}
}
return (string[])versions.ToArray(typeof(string));
}
}
}
References
http://support.microsoft.com/default.aspx?kbid=318785
http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx
http://blogs.msdn.com/astebner/archive/2004/09/18/231253.aspx