It used to be easy, but Microsoft decided to make a breaking change: Before version 4.5, each version of .NET resided in its own directory below C:\Windows\Microsoft.NET\Framework
(subdirectories v1.0.3705, v1.1.4322, v2.0.50727, v3.0, v3.5
and v4.0.30319
).
Since version 4.5 this has been changed: Each version of .NET (i.e. 4.5.x, 4.6.x, 4.7.x, 4.8.x, ...) is being installed in the same subdirectory v4.0.30319
- so you are no longer able to check the installed .NET version by looking into Microsoft.NET\Framework
.
To check the .NET version, Microsoft has provided two different sample scripts depending on the .NET version that is being checked, but I don't like having two different C# scripts for this.
So I tried to combine them into one, here's the script GetDotNetVersion.cs
I created (and updated it for 4.7.1 framework):
using System;
using Microsoft.Win32;
public class GetDotNetVersion
{
public static void Main(string[] args)
{
Console.WriteLine((args != null && args.Length > 0)
? "Command line arguments: " + string.Join(",", args)
: "");
string maxDotNetVersion = GetVersionFromRegistry();
if (String.Compare(maxDotNetVersion, "4.5") >= 0)
{
string v45Plus = GetDotNetVersion.Get45PlusFromRegistry();
if (v45Plus != "") maxDotNetVersion = v45Plus;
}
Console.WriteLine("*** Maximum .NET version number found is: " + maxDotNetVersion + "***");
}
private static string Get45PlusFromRegistry()
{
String dotNetVersion = "";
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
dotNetVersion = CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
Console.WriteLine(".NET Framework Version: " + dotNetVersion);
}
else
{
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
return dotNetVersion;
}
// Checking the version using >= will enable forward compatibility.
private static string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 528040) return "4.8 or later";
if (releaseKey >= 461808) return "4.7.2";
if (releaseKey >= 461308) return "4.7.1";
if (releaseKey >= 460798) return "4.7";
if (releaseKey >= 394802) return "4.6.2";
if (releaseKey >= 394254) return "4.6.1";
if (releaseKey >= 393295) return "4.6";
if ((releaseKey >= 379893)) return "4.5.2";
if ((releaseKey >= 378675)) return "4.5.1";
if ((releaseKey >= 378389)) return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
private static string GetVersionFromRegistry()
{
String maxDotNetVersion = "";
// Opens the registry key for the .NET Framework entry.
using (RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "")
.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
// As an alternative, if you know the computers you will query are running .NET Framework 4.5
// or later, you can use:
// using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
// RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
{
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", "");
string sp = versionKey.GetValue("SP", "").ToString();
string install = versionKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later.
{
Console.WriteLine(versionKeyName + " " + name);
if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
}
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
}
}
if (name != "")
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", "");
if (name != "")
{
sp = subKey.GetValue("SP", "").ToString();
}
install = subKey.GetValue("Install", "").ToString();
if (install == "")
{
//no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
}
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
}
else if (install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name);
if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
} // if
} // if
} // for
} // if
} // foreach
} // using
return maxDotNetVersion;
}
} // class
On my machine it outputs:
v2.0.50727 2.0.50727.4927 SP2
v3.0 3.0.30729.4926 SP2
v3.5 3.5.30729.4926 SP1
v4
Client 4.7.03056
Full 4.7.03056
v4.0
Client 4.0.0.0
.NET Framework Version: 4.7.2 or later
**** Maximum .NET version number found is: 4.7.2 or later ****
The only thing that needs to be maintained over time is the build number once a .NET version greater than 4.7.1 comes out - that can be done easily by modifying the function CheckFor45PlusVersion
, you need to know the release key for the new version then you can add it. For example:
if (releaseKey >= 461308) return "4.7.1 or later";
This release key is still the latest one and valid for the Fall Creators update of Windows 10. If you're still running other (older) Windows versions, there is another one as per this documentation from Microsoft:
.NET Framework 4.7.1 installed on all other Windows OS versions 461310
So, if you need that as well, you'll have to add
if (releaseKey >= 461310) return "4.7.1 or later";
to the top of the function CheckFor45PlusVersion
.
Likewise it works for newer versions. For example, I have added the check for 4.8 recently. You can find those build numbers usually at Microsoft.
Note: You don't need Visual Studio to be installed, not even PowerShell - you can use csc.exe
to compile and run the script above, which I have described here.
Update: The question is about the .NET Framework, for the sake of completeness I'd like to mention how to query the version of .NET Core as well - compared with the above, that is easy: Open a command shell and type:
dotnet --info
Enter
and it will list the .NET Core version number, the Windows version and the versions of each related runtime DLL as well. Sample output:
.NET Core SDK (reflecting any global.json):
Version: 2.1.300
Commit: adab45bf0c
Runtime Environment:
OS Name: Windows
OS Version: 10.0.15063
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.1.300
Host (useful for support):
Version: 2.1.0
Commit: caa7b7e2ba
.NET Core SDKs installed:
1.1.9 [C:\Program Files\dotnet\sdk]
2.1.102 [C:\Program Files\dotnet\sdk]
...
2.1.300 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.0 [C:\Program
Files\dotnet\shared\Microsoft.AspNetCore.All]
...
Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
Note that
if you only need the version number without all the additional information, you can use dotnet --version
.
on a 64 bit Windows PC, it is possible to install the x86 version side by side with the x64 version of .NET Core. If that is the case, you will only get the version that comes first in the environment variable PATH
. That is especially important if you need to keep it up to date and want to know each version. To query both versions, use:
C:\Program Files\dotnet\dotnet.exe --version
3.0.100-preview6-012264
C:\Program Files (x86)\dotnet\dotnet.exe --version
3.0.100-preview6-012264
In the example above, both are the same, but if you forgot to update both instances, you might get different results! Note that Program Files
is for the 64bit version, and Program Files (x86)
is the 32bit version.
Update: If you prefer to keep the build numbers in a list rather than in a cascade of if-statements (as Microsoft suggested), then you can use this code for CheckFor45PlusVersion
instead:
private static string CheckFor45PlusVersion(int releaseKey)
{
var release = new Dictionary<int, string>()
{
{ 378389, "4.5" },
{ 378675, "4.5.1" }, { 379893, "4.5.2" },
{ 393295, "4.6" },
{ 394254, "4.6.1" }, { 394802, "4.6.2" },
{ 460798, "4.7" },
{ 461308, "4.7.1" }, { 461808, "4.7.2" },
{ 528040, "4.8 or later" }
};
int result = -1;
foreach(var k in release.OrderBy(k=>k.Key))
{
if (k.Key <= releaseKey) result = k.Key; else break;
};
return (result > 0) ? release[result] : "No 4.5 or later version detected";
}