C# – How to get the .NET framework version that the application is using

c

I have referred to the Stack Overflow question Is there an easy way to check the .NET Framework version?. But the suggestions given there did not work for the following purpose.

How can we identify the .NET version that the C# console application is using?

Environment:

  • Visual Studio 2010
  • .NET Framework: 3.5 (please see attached screenshot)

CODE

using System;
using System.Globalization;
using Microsoft.Win32;
namespace TESTConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //.NET version: Approach 1
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
            string[] version_names = installed_versions.GetSubKeyNames();
            double latestFramework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);
            int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));
            Console.WriteLine(latestFramework);

            //Approach 2
            string versionval = Environment.Version.ToString();
            Console.WriteLine(versionval);

            //Approach 3
            string systemVersionVal = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion().ToString();
            Console.WriteLine(systemVersionVal);

            Console.ReadLine();
        }
    }
}

Output

Enter image description here

VERSION Setup

Enter image description here

Best Answer

The 2nd and 3rd approached are the CLR version numbers.

.NET Framework 2 and .NET Framework 3.5 are using CLR 2.0.

And there's no CLR 3.0 or 3.5.