Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display detailed version information about the .NET framework installed
using System;
using Microsoft.Win32;
namespace AboutFramework
{
class Program
{
static void Main(string[] args)
{
GetVersionFromRegistry();
Console.WriteLine();
Get45or451FromRegistry();
Console.WriteLine();
GetUpdateHistory();
Console.WriteLine();
Console.Write("Press any key to continue . . .");
Console.ReadKey(true);
Console.WriteLine();
}
private static void GetVersionFromRegistry()
{
// Opens the registry key for the .NET Framework entry.
using (RegistryKey ndpKey =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
// As an alternative, if you know the computers you will query are running .NET Framework 4.5
// or later, you can use:
// using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
// RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
{
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", "");
string sp = versionKey.GetValue("SP", "").ToString();
string install = versionKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
}
}
if (name != "")
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", "");
if (name != "")
sp = subKey.GetValue("SP", "").ToString();
install = subKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
}
else if (install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name);
}
}
}
}
}
}
}
private static void Get45or451FromRegistry()
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
Console.WriteLine("Version: " + CheckFor45DotVersion((int)ndpKey.GetValue("Release")));
}
else
{
Console.WriteLine("Version 4.5 or later is not detected.");
}
}
}
// Checking the version using >= will enable forward compatibility,
// however you should always compile your code on newer versions of
// the framework to ensure your app works the same.
private static string CheckFor45DotVersion(int releaseKey)
{
// Update from https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
if (releaseKey >= 394802)
{
return "4.6.2 or later";
}
if (releaseKey >= 394254)
{
return "4.6.1 or later";
}
if (releaseKey >= 393295)
{
return "4.6 or later";
}
if ((releaseKey >= 379893))
{
return "4.5.2 or later";
}
if ((releaseKey >= 378675))
{
return "4.5.1 or later";
}
if ((releaseKey >= 378389))
{
return "4.5 or later";
}
// This line should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
// From https://msdn.microsoft.com/en-us/library/hh925567(v=vs.110).aspx
private static void GetUpdateHistory()
{
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\Updates"))
{
foreach (string baseKeyName in baseKey.GetSubKeyNames())
{
if (baseKeyName.Contains(".NET Framework") || baseKeyName.StartsWith("KB") || baseKeyName.Contains(".NETFramework"))
{
using (RegistryKey updateKey = baseKey.OpenSubKey(baseKeyName))
{
string name = (string)updateKey.GetValue("PackageName", "");
Console.WriteLine(baseKeyName + " " + name);
foreach (string kbKeyName in updateKey.GetSubKeyNames())
{
using (RegistryKey kbKey = updateKey.OpenSubKey(kbKeyName))
{
name = (string)kbKey.GetValue("PackageName", "");
Console.WriteLine(" " + kbKeyName + " " + name);
if (kbKey.SubKeyCount > 0)
{
foreach (string sbKeyName in kbKey.GetSubKeyNames())
{
using (RegistryKey sbSubKey = kbKey.OpenSubKey(sbKeyName))
{
name = (string)sbSubKey.GetValue("PackageName", "");
if (name == "")
name = (string)sbSubKey.GetValue("Description", "");
Console.WriteLine(" " + sbKeyName + " " + name);
}
}
}
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment