|
|
Comments and Discussions
|
|
 |
|

|
Its in the old version of the code.
Author maybe forgot to compile his code before he posted the update
Needs update for V4.0, 4.5 etc......
|
|
|
|

|
I post the update for the code:
File FrameworkVersion.cs
using System;
namespace Campari.Software
{
#region enum FrameworkVersion
public enum FrameworkVersion
{
Fx10,
Fx11,
Fx20,
Fx30,
Fx35,
Fx40C,
Fx40F
}
#endregion
}
File FrameworkVersionDetection.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Microsoft.Win32;
using Campari.Software.InteropServices;
using System.IO;
using System.Diagnostics;
namespace Campari.Software
{
#region class FrameworkVersionDetection
public static class FrameworkVersionDetection
{
#region class-wide fields
const string Netfx10RegKeyName = "Software\\Microsoft\\.NETFramework\\Policy\\v1.0";
const string Netfx10RegKeyValue = "3705";
const string Netfx10SPxMSIRegKeyName = "Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}";
const string Netfx10SPxOCMRegKeyName = "Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}";
const string Netfx10SPxRegValueName = "Version";
const string Netfx11RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322";
const string Netfx20RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727";
const string Netfx30RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0";
const string Netfx35RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5";
const string Netfx40RegKeyNameClient = "Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client";
const string Netfx40RegKeyNameFull = "Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full";
const string Netfx11PlusRegValueName = "Install";
const string Netfx30PlusRegValueName = "InstallSuccess";
const string Netfx11PlusSPxRegValueName = "SP";
const string Netfx20PlusBuildRegValueName = "Increment";
const string Netfx30PlusVersionRegValueName = "Version";
const string Netfx35PlusBuildRegValueName = "Build";
const string Netfx30PlusWCFRegKeyName = Netfx30RegKeyName + "\\Setup\\Windows Communication Foundation";
const string Netfx30PlusWPFRegKeyName = Netfx30RegKeyName + "\\Setup\\Windows Presentation Foundation";
const string Netfx30PlusWFRegKeyName = Netfx30RegKeyName + "\\Setup\\Windows Workflow Foundation";
const string Netfx30PlusWFPlusVersionRegValueName = "FileVersion";
const string CardSpaceServicesRegKeyName = "System\\CurrentControlSet\\Services\\idsvc";
const string CardSpaceServicesPlusImagePathRegName = "ImagePath";
#endregion
#region private and internal properties and methods
#region methods
#region GetRegistryValue
private static bool GetRegistryValue<T>(RegistryHive hive, string key, string value, RegistryValueKind kind, out T data)
{
bool success = false;
data = default(T);
using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
{
if (baseKey != null)
{
using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree))
{
if (registryKey != null)
{
if (registryKey != null)
{
object regValue = registryKey.GetValue(value, null);
if (regValue != null)
{
RegistryValueKind kindFound = registryKey.GetValueKind(value);
if (kindFound == kind)
{
data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
success = true;
}
}
}
}
}
}
}
return success;
}
#endregion
#region IsNetfxInstalled functions
#region IsNetfx10Installed
private static bool IsNetfx10Installed()
{
string regValue = string.Empty;
return (GetRegistryValue(RegistryHive.LocalMachine, Netfx10RegKeyName, Netfx10RegKeyValue, RegistryValueKind.String, out regValue));
}
#endregion
#region IsNetfx11Installed
private static bool IsNetfx11Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx11RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx20Installed
private static bool IsNetfx20Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx30Installed
private static bool IsNetfx30Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx35Installed
private static bool IsNetfx35Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx40ClientInstalled
private static bool IsNetfx40ClientInstalled()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameClient, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx40FullInstalled
private static bool IsNetfx40FullInstalled()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameFull, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#endregion
#region GetNetfxSPLevel functions
#region GetNetfx10SPLevel
private static int GetNetfx10SPLevel()
{
bool foundKey = false;
int servicePackLevel = -1;
string regValue;
if (IsTabletOrMediaCenter())
{
foundKey = GetRegistryValue(RegistryHive.LocalMachine, Netfx10SPxOCMRegKeyName, Netfx10SPxRegValueName, RegistryValueKind.String, out regValue);
}
else
{
foundKey = GetRegistryValue(RegistryHive.LocalMachine, Netfx10SPxMSIRegKeyName, Netfx10SPxRegValueName, RegistryValueKind.String, out regValue);
}
if (foundKey)
{
int index = regValue.LastIndexOf(',');
if (index > 0)
{
Int32.TryParse(regValue.Substring(index + 1), out servicePackLevel);
}
}
return servicePackLevel;
}
#endregion
#region GetNetfx11SPLevel
private static int GetNetfx11SPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx11RegKeyName, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#region GetNetfx20SPLevel
private static int GetNetfx20SPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#region GetNetfx30SPLevel
private static int GetNetfx30SPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30RegKeyName, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#region GetNetfx35SPLevel
private static int GetNetfx35SPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#region GetNetfx40CSPLevel
private static int GetNetfx40CSPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameClient, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#region GetNetfx40FSPLevel
private static int GetNetfx40FSPLevel()
{
int regValue = 0;
int servicePackLevel = -1;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameFull, Netfx11PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
servicePackLevel = regValue;
}
return servicePackLevel;
}
#endregion
#endregion
#region GetNetfxExactVersion functions
#region GetNetfx10ExactVersion
private static Version GetNetfx10ExactVersion()
{
bool foundKey = false;
Version fxVersion = new Version();
string regValue;
if (IsTabletOrMediaCenter())
{
foundKey = GetRegistryValue(RegistryHive.LocalMachine, Netfx10SPxOCMRegKeyName, Netfx10SPxRegValueName, RegistryValueKind.String, out regValue);
}
else
{
foundKey = GetRegistryValue(RegistryHive.LocalMachine, Netfx10SPxMSIRegKeyName, Netfx10SPxRegValueName, RegistryValueKind.String, out regValue);
}
if (foundKey)
{
int index = regValue.LastIndexOf(',');
if (index > 0)
{
string[] tokens = regValue.Substring(0, index).Split(',');
if (tokens.Length == 3)
{
fxVersion = new Version(Convert.ToInt32(tokens[0], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[1], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[2], NumberFormatInfo.InvariantInfo));
}
}
}
return fxVersion;
}
#endregion
#region GetNetfx11ExactVersion
private static Version GetNetfx11ExactVersion()
{
int regValue = 0;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx11RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
string[] tokens = Netfx11RegKeyName.Split(new string[] { "NDP\\v" }, StringSplitOptions.None);
if (tokens.Length == 2)
{
fxVersion = new Version(tokens[1]);
}
}
}
return fxVersion;
}
#endregion
#region GetNetfx20ExactVersion
private static Version GetNetfx20ExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, Netfx20PlusBuildRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
string[] versionTokens = Netfx20RegKeyName.Split(new string[] { "NDP\\v" }, StringSplitOptions.None);
if (versionTokens.Length == 2)
{
string[] tokens = versionTokens[1].Split('.');
if (tokens.Length == 3)
{
fxVersion = new Version(Convert.ToInt32(tokens[0], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[1], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[2], NumberFormatInfo.InvariantInfo), Convert.ToInt32(regValue, NumberFormatInfo.InvariantInfo));
}
}
}
}
return fxVersion;
}
#endregion
#region GetNetfx30ExactVersion
private static Version GetNetfx30ExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30RegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#region GetNetfx35ExactVersion
private static Version GetNetfx35ExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#region GetNetfx40CExactVersion
private static Version GetNetfx40CExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameClient, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#region GetNetfx40FExactVersion
private static Version GetNetfx40FExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx40RegKeyNameFull, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#endregion
#region WindowsFounationLibrary functions
#region CardSpace
#region IsNetfx30CardSpaceInstalled
private static bool IsNetfx30CardSpaceInstalled()
{
bool found = false;
string regValue = String.Empty;
if (GetRegistryValue(RegistryHive.LocalMachine, CardSpaceServicesRegKeyName, CardSpaceServicesPlusImagePathRegName, RegistryValueKind.ExpandString, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
found = true;
}
}
return found;
}
#endregion
#region GetNetfx30CardSpaceSPLevel
private static int GetNetfx30CardSpaceSPLevel()
{
int servicePackLevel = -1;
return servicePackLevel;
}
#endregion
#region GetNetfx30CardSpaceExactVersion
private static Version GetNetfx30CardSpaceExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, CardSpaceServicesRegKeyName, CardSpaceServicesPlusImagePathRegName, RegistryValueKind.ExpandString, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(regValue.Trim('"'));
int index = fileVersionInfo.FileVersion.IndexOf(' ');
fxVersion = new Version(fileVersionInfo.FileVersion.Substring(0, index));
}
}
return fxVersion;
}
#endregion
#endregion
#region Windows Communication Foundation
#region IsNetfx30WCFInstalled
private static bool IsNetfx30WCFInstalled()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWCFRegKeyName, Netfx30PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region GetNetfx30WCFSPLevel
private static int GetNetfx30WCFSPLevel()
{
int servicePackLevel = -1;
return servicePackLevel;
}
#endregion
#region GetNetfx30WCFExactVersion
private static Version GetNetfx30WCFExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWCFRegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#endregion
#region Windows Presentation Foundation
#region IsNetfx30WPFInstalled
private static bool IsNetfx30WPFInstalled()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWPFRegKeyName, Netfx30PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region GetNetfx30WPFSPLevel
private static int GetNetfx30WPFSPLevel()
{
int servicePackLevel = -1;
return servicePackLevel;
}
#endregion
#region GetNetfx30WPFExactVersion
private static Version GetNetfx30WPFExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWPFRegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#endregion
#region Windows Workflow Foundation
#region IsNetfx30WFInstalled
private static bool IsNetfx30WFInstalled()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWFRegKeyName, Netfx30PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region GetNetfx30WFSPLevel
private static int GetNetfx30WFSPLevel()
{
int servicePackLevel = -1;
return servicePackLevel;
}
#endregion
#region GetNetfx30WFExactVersion
private static Version GetNetfx30WFExactVersion()
{
string regValue = String.Empty;
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30PlusWFRegKeyName, Netfx30PlusWFPlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#endregion
#endregion
#region IsTabletOrMediaCenter
private static bool IsTabletOrMediaCenter()
{
return ((SafeNativeMethods.GetSystemMetrics(SystemMetric.SM_TABLETPC) != 0) || (SafeNativeMethods.GetSystemMetrics(SystemMetric.SM_MEDIACENTER) != 0));
}
#endregion
#endregion
#endregion
#region public properties and methods
#region methods
#region IsInstalled
#region IsInstalled(FrameworkVersion frameworkVersion)
public static bool IsInstalled(FrameworkVersion frameworkVersion)
{
bool ret = false;
switch (frameworkVersion)
{
case FrameworkVersion.Fx10:
ret = IsNetfx10Installed();
break;
case FrameworkVersion.Fx11:
ret = IsNetfx11Installed();
break;
case FrameworkVersion.Fx20:
ret = IsNetfx20Installed();
break;
case FrameworkVersion.Fx30:
ret = IsNetfx30Installed();
break;
case FrameworkVersion.Fx35:
ret = IsNetfx35Installed();
break;
case FrameworkVersion.Fx40C:
ret = IsNetfx40ClientInstalled();
break;
case FrameworkVersion.Fx40F:
ret = IsNetfx40FullInstalled();
break;
default:
break;
}
return ret;
}
#endregion
#region IsInstalled(WindowsFoundationLibrary foundationLibrary)
public static bool IsInstalled(WindowsFoundationLibrary foundationLibrary)
{
bool ret = false;
switch (foundationLibrary)
{
case WindowsFoundationLibrary.CardSpace:
ret = IsNetfx30CardSpaceInstalled();
break;
case WindowsFoundationLibrary.WCF:
ret = IsNetfx30WCFInstalled();
break;
case WindowsFoundationLibrary.WF:
ret = IsNetfx30WFInstalled();
break;
case WindowsFoundationLibrary.WPF:
ret = IsNetfx30WPFInstalled();
break;
default:
break;
}
return ret;
}
#endregion
#endregion
#region GetServicePackLevel
#region GetServicePackLevel(FrameworkVersion frameworkVersion)
public static int GetServicePackLevel(FrameworkVersion frameworkVersion)
{
int servicePackLevel = -1;
switch (frameworkVersion)
{
case FrameworkVersion.Fx10:
servicePackLevel = GetNetfx10SPLevel();
break;
case FrameworkVersion.Fx11:
servicePackLevel = GetNetfx11SPLevel();
break;
case FrameworkVersion.Fx20:
servicePackLevel = GetNetfx20SPLevel();
break;
case FrameworkVersion.Fx30:
servicePackLevel = GetNetfx30SPLevel();
break;
case FrameworkVersion.Fx35:
servicePackLevel = GetNetfx35SPLevel();
break;
case FrameworkVersion.Fx40C:
servicePackLevel = GetNetfx40CSPLevel();
break;
case FrameworkVersion.Fx40F:
servicePackLevel = GetNetfx40FSPLevel();
break;
default:
break;
}
return servicePackLevel;
}
#endregion
#region GetServicePackLevel(WindowsFoundationLibrary foundationLibrary)
public static int GetServicePackLevel(WindowsFoundationLibrary foundationLibrary)
{
int servicePackLevel = -1;
switch (foundationLibrary)
{
case WindowsFoundationLibrary.CardSpace:
servicePackLevel = GetNetfx30CardSpaceSPLevel();
break;
case WindowsFoundationLibrary.WCF:
servicePackLevel = GetNetfx30WCFSPLevel();
break;
case WindowsFoundationLibrary.WF:
servicePackLevel = GetNetfx30WFSPLevel();
break;
case WindowsFoundationLibrary.WPF:
servicePackLevel = GetNetfx30WPFSPLevel();
break;
default:
break;
}
return servicePackLevel;
}
#endregion
#endregion
#region GetExactVersion
#region GetExactVersion(FrameworkVersion frameworkVersion)
public static Version GetExactVersion(FrameworkVersion frameworkVersion)
{
Version fxVersion = new Version();
switch (frameworkVersion)
{
case FrameworkVersion.Fx10:
fxVersion = GetNetfx10ExactVersion();
break;
case FrameworkVersion.Fx11:
fxVersion = GetNetfx11ExactVersion();
break;
case FrameworkVersion.Fx20:
fxVersion = GetNetfx20ExactVersion();
break;
case FrameworkVersion.Fx30:
fxVersion = GetNetfx30ExactVersion();
break;
case FrameworkVersion.Fx35:
fxVersion = GetNetfx35ExactVersion();
break;
case FrameworkVersion.Fx40C:
fxVersion = GetNetfx40CExactVersion();
break;
case FrameworkVersion.Fx40F:
fxVersion = GetNetfx40FExactVersion();
break;
default:
break;
}
return fxVersion;
}
#endregion
#region GetExactVersion(WindowsFoundationLibrary foundationLibrary)
public static Version GetExactVersion(WindowsFoundationLibrary foundationLibrary)
{
Version fxVersion = new Version();
switch (foundationLibrary)
{
case WindowsFoundationLibrary.CardSpace:
fxVersion = GetNetfx30CardSpaceExactVersion();
break;
case WindowsFoundationLibrary.WCF:
fxVersion = GetNetfx30WCFExactVersion();
break;
case WindowsFoundationLibrary.WF:
fxVersion = GetNetfx30WFExactVersion();
break;
case WindowsFoundationLibrary.WPF:
fxVersion = GetNetfx30WPFExactVersion();
break;
default:
break;
}
return fxVersion;
}
#endregion
#endregion
#endregion
#endregion
}
#endregion
}
File form Framework. Sory I convert from c# to vb. You can change easy.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Try
Dim fx10Installed As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx10)
Dim fx11Installed As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx11)
Dim fx20Installed As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx20)
Dim fx30Installed As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx30)
Dim fx35Installed As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx35)
Dim fx40CInstalled As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx40C)
Dim fx40FInstalled As Boolean = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx40F)
treeView1.Nodes.Add(Environment.MachineName)
Dim tn As TreeNode
If fx10Installed Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 1.0"
tn.Text = ".NET Framework 1.0"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx10).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx10).ToString)
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 1.0"
tn.Text = ".NET Framework 1.0"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx11Installed Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 1.1"
tn.Text = ".NET Framework 1.1"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx11).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx11))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 1.1"
tn.Text = ".NET Framework 1.1"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx20Installed Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 2.0"
tn.Text = ".NET Framework 2.0"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx20).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx20))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 2.0"
tn.Text = ".NET Framework 2.0"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx30Installed Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 3.0"
tn.Text = ".NET Framework 3.0"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx30).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx30))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 3.0"
tn.Text = ".NET Framework 3.0"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx35Installed Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 3.5"
tn.Text = ".NET Framework 3.5"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx35).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx35))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 3.5"
tn.Text = ".NET Framework 3.5"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx40CInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 4.0C"
tn.Text = ".NET Framework 4.0 Client"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx40C).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx40C))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 4.0C"
tn.Text = ".NET Framework 4.0 Client"
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx40FInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = ".NET Framework 4.0F"
tn.Text = ".NET Framework 4.0 Full"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx40F).ToString)
tn.Nodes.Add("Service Pack: " & FrameworkVersionDetection.GetServicePackLevel(FrameworkVersion.Fx40F))
treeView1.Nodes(0).Nodes.Add(tn)
Else
tn = New TreeNode()
tn.ForeColor = Color.Red
tn.Name = ".NET Framework 4.0F"
tn.Text = ".NET Framework 4.0 Full"
treeView1.Nodes(0).Nodes.Add(tn)
End If
Dim fx30PlusWCFInstalled As Boolean = FrameworkVersionDetection.IsInstalled(WindowsFoundationLibrary.WCF)
Dim fx30PlusWPFInstalled As Boolean = FrameworkVersionDetection.IsInstalled(WindowsFoundationLibrary.WPF)
Dim fx30PlusWFInstalled As Boolean = FrameworkVersionDetection.IsInstalled(WindowsFoundationLibrary.WF)
Dim fx30PlusCardSpacesInstalled As Boolean = FrameworkVersionDetection.IsInstalled(WindowsFoundationLibrary.CardSpace)
If fx30PlusWCFInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = "WCF"
tn.Text = "WCF"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(WindowsFoundationLibrary.WCF).ToString)
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx30PlusWPFInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = "WPF"
tn.Text = "WPF"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(WindowsFoundationLibrary.WPF).ToString)
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx30PlusWFInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = "WF"
tn.Text = "WF"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(WindowsFoundationLibrary.WF).ToString)
treeView1.Nodes(0).Nodes.Add(tn)
End If
If fx30PlusCardSpacesInstalled Then
tn = New TreeNode()
tn.ForeColor = Color.Green
tn.Name = "Card Space"
tn.Text = "Card Space"
tn.Nodes.Add("Exact Version: " & FrameworkVersionDetection.GetExactVersion(WindowsFoundationLibrary.CardSpace).ToString)
treeView1.Nodes(0).Nodes.Add(tn)
End If
treeView1.Nodes(0).Expand()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
I hope that is helpfull
modified on Wednesday, April 27, 2011 6:34 AM
|
|
|
|

|
Yes, very helpful... you missed some things in your code, however, when you cut and pasted it:
First, you also need the using statements:
using System.Collections;
using System.Diagnostics.CodeAnalysis;
Then, in the FrameworkVersionDetection Class, you need to add a Properties Section to the
#region public properties and methods
That looks like this:
#region properties
#region InstalledFrameworkVersions
public static IEnumerable InstalledFrameworkVersions
{
get
{
if (IsInstalled(FrameworkVersion.Fx10))
{
yield return GetExactVersion(FrameworkVersion.Fx10);
}
if (IsInstalled(FrameworkVersion.Fx11))
{
yield return GetExactVersion(FrameworkVersion.Fx11);
}
if (IsInstalled(FrameworkVersion.Fx20))
{
yield return GetExactVersion(FrameworkVersion.Fx20);
}
if (IsInstalled(FrameworkVersion.Fx30))
{
yield return GetExactVersion(FrameworkVersion.Fx30);
}
if (IsInstalled(FrameworkVersion.Fx35))
{
yield return GetExactVersion(FrameworkVersion.Fx35);
}
}
}
#endregion
#endregion
That compiles and works.
Anyone who doesn't like dropping into VB, can also test this new code by going into the PROGRAM.CS file that Scott included, and adding the following lines:
bool fx40CInstalled = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx40C);
bool fx40FInstalled = FrameworkVersionDetection.IsInstalled(FrameworkVersion.Fx40F);
Console.WriteLine(".NET Framework 4.0 Client installed? {0}", fx40CInstalled);
Console.WriteLine(".NET Framework 4.0 FULL installed? {0}", fx40FInstalled);
|
|
|
|

|
I compared the code of your version of FrameworkVersionDetection.cs to the original code and there are a lot of differences. Why the big difference?
|
|
|
|

|
not it's too much. Only add new procedures to detect the new framework.
If you detect more changes that you expect, it's posible that the author change after some code.
|
|
|
|

|
where is InstalledFrameworkVersions in sourcecode ??
foreach (Version fxVersion in FrameworkVersionDetection.InstalledFrameworkVersions)
{
Console.WriteLine(fxVersion .ToString());
}
AE
|
|
|
|
|

|
Any chance you will be able to update your code to cover Framework 4.0??
Great article and software. Thanks for sharing.
|
|
|
|

|
Yes, I will be updating this to work with .NET 4.0 soon.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
any update ??? for 4.0 ?
AE
|
|
|
|

|
It has been over a year now, have you had time to update your code????
|
|
|
|

|
/// <summary>
/// 4.0 Client Profile
/// </summary>
const string Netfx40RegKeyNameClient = "Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client";
/// <summary>
/// 4.0 Full Profile
/// </summary>
const string Netfx40RegKeyNameFull = "Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full";
|
|
|
|

|
Thank you Marco for the reply to my post. Unfortunately, I am not a C# programmer and it would take a lot more than those two constants for me to modify Scott Dorman's code. I will have to wait for him to update his code and post it.
|
|
|
|

|
I'm hoping to have some time to update this by the end of next week.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
Thanks for your reply Scott.
I went through the code as much as I could to see if I could use Marco's suggestion and realize that it is quiet involved and will take a little time to complete. I am sure you have important things like job and family that should come first. Take your time.
|
|
|
|
|

|
Do you think it's possible to run this against another machine on my network? I've got a couple of hundred machines that I need to check to see if they have 3.5 SP1 installed. Would it be possible to point this app at a remote machine, do you think?
You know what ol' Jack Burton says at a time like this...
|
|
|
|
|
|

|
Any update about the 4.0 Framework check?
Thanks,
Nadin
|
|
|
|

|
Thanks for posting this useful article...
I tested your library on Vista and XP with all updates installed till now, but it is returning different version information.
If you need more details then kindly send me msg and I will provide the same. ( I have used contact form on your blog also to post this same msg)
I wanted to request if there is a possibility of fixing this issue.
Thanks in advance..
|
|
|
|

|
What issues are you running in to? This was developed on an XP machine that was then upgraded to Vista and I haven't seen any issues. If I can narrow down the problem I'll do my best to fix it in the article and code.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|

|
Thanks for replying..
I'm getting following values on Vista. I have just formatted it for your convenience.
Net Framework 2 Ver : 2.0.50727.4016 - Service Pack 2
Net Framework 3 Ver : 3.0.30729.4037 - Service Pack 2
Net Framework 3.5 Ver : 3.5.30729.1 - Service Pack 1
and following values on XP.
Net Framework 2 Ver : 2.2.30729 - Service Pack 2
Net Framework 3 Ver : 3.2.30729 - Service Pack 2
Net Framework 3.5 Ver : 3.5.30729.1 - Service Pack 1
You can see that only for 3.5 framework it is returning identical values. For version 2 and 3 it is returning incorrect version. Service Pack Info is correct for all frameworks.
|
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Explains how to use managed code to detect which .NET Framework versions and service packs are installed.
| Type | Article |
| Licence | CPOL |
| First Posted | 3 Feb 2007 |
| Views | 142,525 |
| Downloads | 1,715 |
| Bookmarked | 165 times |
|
|