// 程序启动函数, 判断当前系统中的DotNet版本是否安装有DotNet3.5及其以上版本
public void Main()
{
var verMin = new System.Version(3, 5);
foreach (var ver in InstalledDotNetVersions())
{
if (ver >= verMin)
{
break;
}
}
}
// 静态函数
public static System.Collections.ObjectModel.Collection<Version> InstalledDotNetVersions()
{
var versions = new System.Collections.ObjectModel.Collection<Version>();
var NDPKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
if (NDPKey != null)
{
var subkeys = NDPKey.GetSubKeyNames();
foreach (var subkey in subkeys)
{
GetDotNetVersion(NDPKey.OpenSubKey(subkey), subkey, versions);
GetDotNetVersion(NDPKey.OpenSubKey(subkey).OpenSubKey("Client"), subkey, versions);
GetDotNetVersion(NDPKey.OpenSubKey(subkey).OpenSubKey("Full"), subkey, versions);
}
}
return versions;
}
private static void GetDotNetVersion(Microsoft.Win32.RegistryKey parentKey, string subVersionName, System.Collections.ObjectModel.Collection<Version> versions)
{
if (parentKey != null)
{
var installed = Convert.ToString(parentKey.GetValue("Install"));
if (installed == "1")
{
var version = Convert.ToString(parentKey.GetValue("Version"));
if (string.IsNullOrEmpty(version))
{
if (subVersionName.StartsWith("v"))
{
version = subVersionName.Substring(1);
}
else
{
version = subVersionName;
}
}
var ver = new Version(version);
if (!versions.Contains(ver))
{
versions.Add(ver);
}
}
}
}