用代码检查DotNet 3.5或者4.0是否安装

最近有个需求,需要验证客户端是否安装了DotNet 3.5。一开始本来打算用DotNetmscoree.dll里的GetCORVersion判断,不过实际测试了一下,发现GetCORVersion返回的是的当前系统CLR的版本号,由于DotNet 3.5本质上还是CLR 2.0,所以返回的结果是2.0。

爬网半天,发现还是用注册表的方式最稳定。主要思路是:DotNet 3.5安装完毕后,会在注册表中创建HKLMSOFTWAREMicrosoftNET Framework SetupNDPv3.5,如果这个目录下有个Install的key等于1,那说明3.5已经安装。

Code Snippet
  1. public static bool IsDotNet35Installed()
  2. {
  3.     bool dotNet35Installed = false;
  4.     string dotnet35KeyName = @”SoftwareMicrosoftNET Framework SetupNDPv3.5″;
  5.     string dotnet35ValueName = “Install”;
  6.     string dotnet35Value = GetRegValue(dotnet35KeyName, dotnet35ValueName);
  7.     if (dotnet35Value == “1”)
  8.     {
  9.         dotNet35Installed = true;
  10.     }
  11.     return dotNet35Installed;
  12. }

同理,如果要检查DotNet 4.0是否安装,只需要把对应的key修改成HKLMSOFTWAREMicrosoftNET Framework SetupNDPv4Full。

Leave a Reply

Your email address will not be published. Required fields are marked *