因為佈署前要確定客戶生產環境上幾台機器的.NET Framework版本,上一次筆記查詢.NET Framework Command line、PowerShell方法,這一次來筆記用c#取Registry來判讀。
上一次筆記
[PowerShell]取得Windows .NET Framework版本資訊
1.程式using Microsoft.Win32
using Microsoft.Win32;
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.");
}
}
}
private static string CheckFor45DotVersion(int releaseKey)
{
if (releaseKey >= 393295)
{
return string.Format("4.6 or later ,releaseKey = {0}", releaseKey);
}
if ((releaseKey >= 379893))
{
return string.Format("4.5.2 or later ,releaseKey = {0}", releaseKey);
}
if ((releaseKey >= 378675))
{
return string.Format("4.5.1 or later ,releaseKey = {0}", releaseKey);
}
if ((releaseKey >= 378389))
{
return string.Format("4.5 or later ,releaseKey = {0}", releaseKey);
}
// This line should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return string.Format("No 4.5 or later version detected,releaseKey = {0}", releaseKey);
}
3.測試MBP
別忘了這支偵測版本的工具程式得較早的.NET 4.0編譯,不然工具就自己就沒法執行。
端午節連假第一天!
參考:
How to: Determine Which .NET Framework Versions Are Installed
http://stackoverflow.com/questions/1565434/how-to-find-the-net-version