1

I had a project which first target was .NET4.0 but now I change the project to 4.5.1, and I would like to know how to check the version of .NET, because I run the application in a fresh win7 and the application runs.

I try to use this code to check the version of .NET that is used:

string version = System.Reflection.Assembly
                     .GetExecutingAssembly()
                     .GetReferencedAssemblies()
                     .Where(x => x.Name == "System.Core").First().Version.ToString();

            MessageBox.Show(version);

And the result is 4.0.0.0, so how I am thinking that the application is still 4.0. How can I change the target of the application?

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • possible duplicate of [.NET - what version of the framework am I currently running in (from C#)](http://stackoverflow.com/questions/3704449/net-what-version-of-the-framework-am-i-currently-running-in-from-c) – default Oct 21 '14 at 11:42

1 Answers1

1

Check this if it helps. I have tested it in a web application. It's showing correct values.

var targetFrameWork =  Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute),false);
var frameWork = ((System.Runtime.Versioning.TargetFrameworkAttribute)(targetFrameWork[0])).FrameworkName;

It returns '.NETFramework,Version=v4.5'.

Note:

Environment.Version returns '4.0.30319.18444'.

    System.Reflection.Assembly.GetExecutingAssembly()
                             .GetReferencedAssemblies()
                             .Where(x => x.Name == "System.Core").First().Version.ToString();

It returns '4.0.0.0'.

Satyajit
  • 2,150
  • 2
  • 15
  • 28