C#检验IIS版本、SQL Server版本、SilverLight版本

时间:2021-10-05 10:41:34

之前做一个小项目,使用C#检验这些软件的版本,到处找了一些代码,也能作用,记录一下,以防以后用到。

一、检验IIS版本

public static bool checkIIS(string destVersion)
{
try
{
RegistryKey rk = Registry.LocalMachine;
RegistryKey ver = rk.OpenSubKey(@"SOFTWARE\Microsoft\InetStp");
int majorVersion = Convert.ToInt32(ver.GetValue("majorversion"));
int minorVersion = Convert.ToInt32(ver.GetValue("minorversion"));
Version versionStr = new Version(majorVersion + "." + minorVersion);
if (versionStr >= new Version(destVersion))
return true;
return false;
}
catch
{
return false;
}
}

二、检验SQL Server版本

public static bool checkSQLServer(string destVersion)
{
RegistryKey localKey;
if (Environment.Is64BitOperatingSystem)
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
else
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); RegistryKey sub = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
object keyInst = null;
if (sub != null)
keyInst = sub.GetValue("InstalledInstances"); if (keyInst != null)
{
try
{
Version SQLVer = null;
foreach (string str in (string[])keyInst)
{
RegistryKey subSQL = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");
object keySQL = subSQL.GetValue(str);
RegistryKey subVer = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\" + (string)keySQL + @"\Setup");
object keyVer = subVer.GetValue("Version");
//get version numer
SQLVer = new Version((string)keyVer);
}
//SQL Version should >= 12.1.4100.1
Version tagVer = new Version(destVersion);
if (SQLVer >= tagVer)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
} }
else
return false;
}

三、检验SilverLight版本

public static bool checkSilverLight(string destVersion)
{
try
{
Version version;
RegistryKey rk = Registry.LocalMachine;
RegistryKey ver = rk.OpenSubKey(@"SOFTWARE\Microsoft\Silverlight");
if (ver == null)
ver = rk.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Silverlight");
version = new Version(ver.GetValue("Version").ToString()); //version >= 5.1
if (version >= new Version(destVersion))
return true;
return false;
}
catch
{
return false;
}
}