简体   繁体   English

获取当前的.NET Framework版本

[英]To get the current .NET Framework version

I build my C# project with .NET 3.5 SP1 Platform. 我使用.NET 3.5 SP1平台构建了C#项目。 My code will be compiled and for other .NET platform versions too (but later). 我的代码也将被编译,也将被编译为其他.NET平台版本(但以后)。 I need to get the current .NET Framework version in my code. 我需要在代码中获取当前的.NET Framework版本。 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. 我了解.NET 3.0和3.5基于.NET 2.0,但是我需要获得当前平台的确切版本,而不是基本平台版本。

  1. Why did I get (via the Environment.Version ) 2.0.50727.5477 instead of 3.5.30729.5420 ? 为什么我(通过Environment.Version )得到2.0.50727.5477而不是3.5.30729.5420
  2. Why does the current version have .5477 instead of .5420 in the last number of the version string? 为什么当前版本的版本字符串的最后一个数字为.5477而不是.5420
  3. How can I get the exact version of the current .NET Framework? 如何获得当前.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();
}

Check that all projects in your solution are targeting the right version of .net framework. 检查解决方案中的所有项目是否都针对正确版本的.net Framework。

It's the Environment.Version property you are looking for: 您正在寻找的Environment.Version属性:

http://msdn.microsoft.com/en-us/library/system.environment.version%28v=vs.110%29.aspx 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());

Your answer is here: https://msdn.microsoft.com/en-us/library/bb822049(v=vs.110).aspx 您的答案在这里: https : //msdn.microsoft.com/zh-cn/library/bb822049(v=vs.110).aspx

The .NET Framework version is not the same as the CLR version. .NET Framework版本与CLR版本不同。 The CLR is just a component of the .NET Framework. CLR只是.NET Framework的组件。 So the .NET Framwork versions 3.0 and 3.5 use the CLR version 2.0. 因此,.NET Framwork版本3.0和3.5使用CLR版本2.0。

Your test program is getting the exact versions of the .NET Framework. 您的测试程序正在获取.NET Framework的确切版本。 But it incorrectly labels Environment.Version as the "Current .NET Framework version" when it is really the "Current CLR version". 但是当它实际上是“当前CLR版本”时,它错误地将Environment.Version标记为“当前.NET Framework版本”。 The link provided by @Andrew states this value as: @Andrew提供的链接将此值声明为:

Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime. 获取一个Version对象,该对象描述公共语言运行时的主要,次要,内部和修订版本号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM