Skip to content

Instantly share code, notes, and snippets.

@embix
Created July 7, 2015 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embix/5745130ba9b93c7b0200 to your computer and use it in GitHub Desktop.
Save embix/5745130ba9b93c7b0200 to your computer and use it in GitHub Desktop.
CheckForDotNet45, patched to make it work on windows server core
using Microsoft.Win32;
using System;
using System.Diagnostics;
// originally from https://github.com/shanselman/SmallestDotNet/tree/master/CheckForDotNet45
// patched to work even on windows server core
namespace CheckForDotNet45
{
internal class Program
{
private const string site = "http://smallestdotnet.com/?realversion={0}";
public Program()
{
}
private static string Get45or451FromRegistry()
{
string str;
RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\");
try
{
int value = (int)registryKey.GetValue("Release");
if (value == 378389)
{
str = "4.5";
}
else if (!(value == 378758 ? false : value != 378675))
{
str = "4.5.1";
}
else if (value != 379893)
{
str = "4.5";
return str;
}
else
{
str = "4.5.2";
}
}
finally
{
if (registryKey != null)
{
((IDisposable)registryKey).Dispose();
}
}
return str;
}
public static bool IsNet45OrNewer()
{
return Type.GetType("System.Reflection.ReflectionContext", false) != null;
}
private static void Main(string[] args)
{
Console.Write("Checking .NET version...");
if (!Program.IsNet45OrNewer())
{
Console.WriteLine(Environment.Version.ToString());
}
else
{
Console.WriteLine(Program.Get45or451FromRegistry());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment