After searching on google for a simple method to get the current installed version of the .NET Framework, I decided to implement my own. I have put this method in its own class, because there are probably a lot of other similar methods that could be grouped together.
using System;
using System.IO;
using System.Security;
using System.Text.RegularExpressions;
namespace YourNameSpace
{
public class SystemInfo
{
private const string FRAMEWORK_PATH = "\\Microsoft.NET\\Framework";
private const string WINDIR1 = "windir";
private const string WINDIR2 = "SystemRoot";
public static string FrameworkVersion
{
get
{
try
{
return getHighestVersion(NetFrameworkInstallationPath);
}
catch (SecurityException)
{
return "Unknown";
}
}
}
private static string getHighestVersion(string installationPath)
{
string[] versions = Directory.GetDirectories(installationPath, "v*");
string version = "Unknown";
for (int i = versions.Length - 1; i >= 0; i--)
{
version = extractVersion(versions[i]);
if (isNumber(version))
return version;
}
return version;
}
private static string extractVersion(string directory)
{
int startIndex = directory.LastIndexOf("\\") + 2;
return directory.Substring(startIndex, directory.Length - startIndex);
}
private static bool isNumber(string str)
{
return new Regex(@"^[0-9]+\.?[0-9]*$").IsMatch(str);
}
public static string NetFrameworkInstallationPath
{
get { return WindowsPath + FRAMEWORK_PATH; }
}
public static string WindowsPath
{
get
{
string winDir = Environment.GetEnvironmentVariable(WINDIR1);
if (String.IsNullOrEmpty(winDir))
winDir = Environment.GetEnvironmentVariable(WINDIR2);
return winDir;
}
}
}
}
How it works? It searches the .NET Framework installation folder and returns the highest version installed on the system based on the folder names. I think the code is straightforward. I’d like to know other people’s thoughts and ideas. I know about other ways (registry keys, etc). I also hope this code helps other people to save some time.
Sphere: Related ContentTags: .NET, C#, installation, Path, Version


March 19th, 2009 at 3:40 am
I used your code to detect all installed versions, and it seems to work ok.
However, teh code you posted above will not always return you the last isntalled version.
If I get all versions, I get the following result:
1.0.3705
1.1.4322
2.0.50727
3.0
3.5
JSharp
With your code above, it will return me that the highest installed framework is JSharp…
March 19th, 2009 at 1:25 pm
Kclement, thanks for your comment.
I’ve just modified the code above. I think this new version of the class should work in your case, and with luck in any other case.
This new version also provides public properties to get the name of the directory where Windows is installed and the name of the directory where the .NET Framework is installed.
October 22nd, 2009 at 6:03 am
Hi,
Your code actually runs on a big assumption that the frameworks will always be installed in a directory starting with ‘v’. I hope this never changes but when it does, the code will fail and some patches will be required.
The other way could be by reading registry. Again assumption is the same that MS does not change the way they save registries for frameworks.
October 31st, 2009 at 5:34 am
Your code fails when version numbers are or the form
vX.XX.XXXX
So if some one has only upto version 2.0.xxxx installed, your code fails and returns the wrong version.