關於.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_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP
.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的子集,更輕量,更小