using System; using Microsoft.Win32; namespace Developpez.Dotnet.System.Providers.Windows { /// /// Infrastructure (Interop) /// internal static class FrameworkVersionRegistryHelper { #region Nom des clefs du registre const string g_szNetfx20RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727"; const string g_szNetfx30RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup"; const string g_szNetfx30SpRegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0"; const string g_szNetfx30RegValueName = "InstallSuccess"; const string g_szNetfx35RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5"; const string g_szNetfxStandardRegValueName = "Install"; const string g_szNetfxStandardSPxRegValueName = "SP"; const string g_szNetfxStandardVersionRegValueName = "Version"; #endregion #region Utilitaires du registre internal static bool RegistryTryGetValue(RegistryKey root, string key_name, string value_name, out object value) { if (root == null) throw new ArgumentNullException("root"); value = null; RegistryKey key = root; if (!String.IsNullOrEmpty(key_name)) { key = root.OpenSubKey(key_name, false); } if (key == null) return false; else { try { /* il faut bien vérifier si la clef existe */ foreach (string item in key.GetValueNames()) { if ((String.IsNullOrEmpty(item) && String.IsNullOrEmpty(value_name)) || item.Equals(value_name, StringComparison.OrdinalIgnoreCase)) { /* la valeur existe bien */ value = key.GetValue(value_name); return true; } } } finally { if (key != root) key.Close(); /* si key == root ça veux dire que key_name était vide */ } } return false; } #endregion internal static bool IsFX35Installed() { object value = null; if (RegistryTryGetValue(Registry.LocalMachine, g_szNetfx35RegKeyName, g_szNetfxStandardRegValueName, out value)) { /* un peux sale */ try { return (value != null) && (Convert.ToInt32(value) == 1); } catch { return false; } } return false; } internal static bool IsFX30Installed() { object value = null; if (RegistryTryGetValue(Registry.LocalMachine, g_szNetfx30RegKeyName, g_szNetfxStandardRegValueName, out value)) { /* un peux sale */ try { return (value != null) && (Convert.ToInt32(value) == 1); } catch { return false; } } return false; } internal static bool IsFX20Installed() { object value = null; if (RegistryTryGetValue(Registry.LocalMachine, g_szNetfx20RegKeyName, g_szNetfxStandardRegValueName, out value)) { /* un peux sale */ try { return (value != null) && (Convert.ToInt32(value) == 1); } catch { return false; } } return false; } internal static int GetFX35SPLevel() { return GetFXSpLevel(g_szNetfx35RegKeyName, g_szNetfxStandardSPxRegValueName); } internal static int GetFX30SPLevel() { return GetFXSpLevel(g_szNetfx35RegKeyName, g_szNetfxStandardSPxRegValueName); } internal static int GetFX20SPLevel() { return GetFXSpLevel(g_szNetfx35RegKeyName, g_szNetfxStandardSPxRegValueName); } private static int GetFXSpLevel(string regKeyName, string regValueName) { object value = null; if (RegistryTryGetValue(Registry.LocalMachine, regKeyName, regValueName, out value)) { try { return (int)value; } catch { return 0; } } return 0; } } }