c# .NET Framework 版本确定

关于.NET Framework 版本信息这里做个介绍:

1. 编译时,工程的目标的 .NET Framework 版本

同样的代码,我先选择.net 4.0,就发现有语法错误,原因是4.0版本还没提供这个API

改选 .net 4.5,语法错误消失了,因为这个API在后续的 .net已经添加了

 

 但是运行起来,真正使用的 .NET Framework版本就不一定是目标版本了。

2. 运行时实际的Framework版本

真实运行的Framework版本跟机器安装的 Framework 版本有关,如果目标是4.5,机器上只装了 4.8。

那么由于向下兼容的原则,运行时CLR就会使用4.8去运行程序。如果机器只安装了3.5,那么CLR就会弹出一个提示框让用户去安装 4.5。

如何确定机器安装的 Framework版本呢?一般都是查看注册表:

输入 regedit 打开注册表,并找到这一项:

计算机HKEY_LOCAL_MACHINESOFTWAREMicrosoftNET Framework SetupNDP

.NET Framework 4.5之前的版本可以这样依次确认:

4.5以及之后的版本需要这样确认:

Release的值对应了 .NET Framework版本,下面是微软列举每个版本的值

 .NET Framework version  Value of the Release DWORD
.NET Framework 4.5 All Windows operating systems: 378389
.NET Framework 4.5.1 On Windows 8.1 and Windows Server 2012 R2: 378675
On all other Windows operating systems: 378758
.NET Framework 4.5.2 All Windows operating systems: 379893
.NET Framework 4.6 On Windows 10: 393295
On all other Windows operating systems: 393297
.NET Framework 4.6.1 On Windows 10 November Update systems: 394254
On all other Windows operating systems (including Windows 10): 394271
.NET Framework 4.6.2 On Windows 10 Anniversary Update and Windows Server 2016: 394802
On all other Windows operating systems (including other Windows 10 operating systems): 394806
.NET Framework 4.7 On Windows 10 Creators Update: 460798
On all other Windows operating systems (including other Windows 10 operating systems): 460805
.NET Framework 4.7.1 On Windows 10 Fall Creators Update and Windows Server, version 1709: 461308
On all other Windows operating systems (including other Windows 10 operating systems): 461310
.NET Framework 4.7.2 On Windows 10 April 2018 Update and Windows Server, version 1803: 461808
On all Windows operating systems other than Windows 10 April 2018 Update and Windows Server, version 1803: 461814
.NET Framework 4.8 On Windows 10 May 2019 Update and Windows 10 November 2019 Update: 528040
On all other Windows operating systems (including other Windows 10 operating systems): 528049

3. 关于CLR版本

除了.NET Framework的assembly,还有CLR(公共语言运行时),它负责管理执行code,它的版本需要这样确认:

输入命令:clrver

CLR 2.0可以管理 .NET Framework 2.0 3.0 3.5

CLR 4.0可以管理 .NET Framework 4.0 4.5 4.5以上版本

4. 其他

.net 3.5 需要开启windows功能才能正常使用,如下图:

用code 辨识版本号

// 工程配置的目标Framework版本
Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName);
// 实际真实运行的Framework版本
Console.WriteLine(FileVersionInfo.GetVersionInfo(typeof(Uri).Assembly.Location).ProductVersion);

 结果

.NET 4.0 和 .NET 4.0 Client Profile 版本的区别:

4 Client Profile 是.4的子集,更轻量,更小

原文地址:https://www.cnblogs.com/chenyingzuo/p/12003513.html