Andrey Bushman
Andrey Bushman

Reputation: 12516

To get the current .NET Framework version

I build my C# project with .NET 3.5 SP1 Platform. My code will be compiled and for other .NET platform versions too (but later). I need to get the current .NET Framework version in my code. Console output (information about the installed versions I get from the registry):

Installed .NET Framework versions:
v2.0.50727      2.0.50727.5420   SP2
v3.0    3.0.30729.5420   SP2
v3.5    3.5.30729.5420   SP1
v4.5.1 Client   4.5.50938
v4.5.1 Full     4.5.50938
v4.0 Client     4.0.0.0
***
Current .NET Framework version: 2.0.50727.5477
Press any key for exit...

I understand the .NET 3.0 and 3.5 are based on .NET 2.0, but I need to get an exact version of current platform, instead of base platform version.

  1. Why did I get (via the Environment.Version) 2.0.50727.5477 instead of 3.5.30729.5420?
  2. Why does the current version have .5477 instead of .5420 in the last number of the version string?
  3. How can I get the exact version of the current .NET Framework?

My code is based on this article:

static void Main(string[] args) {
  NetFrameworkInfo[] frameworks = 
    ExtendedEnvironment.GetInstalledNetFameworkVersions();
  Console.WriteLine("Installed .NET Framework versions:");
  foreach (NetFrameworkInfo item in frameworks) {
    Console.WriteLine(item);
  }
  Console.WriteLine("***");
  Version version = Environment.Version;
  Console.WriteLine("Current .NET Framework version: {0}", version);
  Console.WriteLine("Press any key for exit...");
  Console.ReadKey();
}

Upvotes: 1

Views: 3664

Answers (2)

Donald Byrd
Donald Byrd

Reputation: 7788

Your answer is here: https://msdn.microsoft.com/en-us/library/bb822049(v=vs.110).aspx

The .NET Framework version is not the same as the CLR version. The CLR is just a component of the .NET Framework. So the .NET Framwork versions 3.0 and 3.5 use the CLR version 2.0.

Your test program is getting the exact versions of the .NET Framework. But it incorrectly labels Environment.Version as the "Current .NET Framework version" when it is really the "Current CLR version". The link provided by @Andrew states this value as:

Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.

Upvotes: 0

Andrew
Andrew

Reputation: 3816

Check that all projects in your solution are targeting the right version of .net framework.

It's the Environment.Version property you are looking for:

http://msdn.microsoft.com/en-us/library/system.environment.version%28v=vs.110%29.aspx

Version ver = Environment.Version;
Console.WriteLine("CLR Version {0}", ver.ToString());

Upvotes: 2

Related Questions