1

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();
}
6
  • It is the kind of problem you never want to need to solve. Particularly if you now enumerate "4.5.1 Client", it doesn't exist. Only the major.minor.build numbers are stable, the revision number can arbitrarily change when updates and security patches are delivered through Windows Update. Commented Apr 15, 2014 at 13:47
  • Example: The AutoCAD 2009 application use the .Net 3.0, but it can to use .net 3.5 if it was installed also. Some my plugins compiled for the .Net 3.5 SP1 platform. In the AutoCAD I must to check the current .NET version and if it is 3.5 SP1 - I can to download my additional plugins. They will not work with 3.0. So I need to get exact version of current .Net Framework. Commented Apr 15, 2014 at 13:57
  • So just look in the registry to see if 3.5 is present, you don't care about the version number at all. Alberto showed you how to do it. Commented Apr 15, 2014 at 14:01
  • I know how to get the all installed .Net Framework versions (it is not a problem). But why the Environment.Version return the invalid (not the exact) version? Commented Apr 15, 2014 at 14:05
  • It returns the runtime version. Doesn't have anything to do with the .NET Framework version, everything to do with the CLR version. Which is v2.0.50727 for all framework versions between 2.0 and 3.5SP1. It is v4.0.30319 for all later versions. Do note that you will get version 4 in AutoCAD 2012, testing your plugins to see if they still operate correctly on .NET 4 is necessary. The "not exact" clause was already covered in my previous comment. Commented Apr 15, 2014 at 14:08

2 Answers 2

2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

You read my topic inattentively (I use it in the last output string).
msdn.microsoft.com/en-us/library/vstudio/… there are 4 clr versions so far: 1.0, 1.1, 2.0, 4. This is what made you confused.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.