using System; using Common.Properties; using Microsoft.Win32; namespace Installer.Common { public static class PreviousInstall { public static string GetMostRecentlyInstalledSubKeyName(Version installingVersion, out string mostRecentlyInstalledLowerVersion) { mostRecentlyInstalledLowerVersion = string.Empty; var mostRecentlyInstalledSubKeyName = string.Empty; var rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); var sk1 = rk.OpenSubKey(Settings.Default.RegistrySoftwareInstalledProducts); var maxProductVersion = new Version(0, 0, 0); if (sk1 == null) return string.Empty; foreach (var productSubKeyName in sk1.GetSubKeyNames()) { var newKey = sk1.OpenSubKey(productSubKeyName); if (newKey == null) continue; var newSubKey = newKey.OpenSubKey(Settings.Default.RegistryInstallProperties); if (newSubKey == null) continue; var val = newSubKey.GetValue(Settings.Default.RegistryDisplayName, -1, RegistryValueOptions.None).ToString(); if ((val == "-1") || (val != Settings.Default.RegistryDataPRO)) continue; var strThisVersion = newSubKey.GetValue(Settings.Default.RegistryDisplayVersion, -1, RegistryValueOptions.None).ToString(); var thisVersion = new Version(strThisVersion); if (thisVersion.IsGreaterThan(maxProductVersion) && thisVersion.IsLessThan(installingVersion)) { maxProductVersion = thisVersion; mostRecentlyInstalledLowerVersion = thisVersion.ToString(); mostRecentlyInstalledSubKeyName = productSubKeyName; } } return mostRecentlyInstalledSubKeyName; } public static bool IsGreaterThan(this Version leftString, Version rightString) { return leftString.CompareTo(rightString) > 0; } public static bool IsLessThan(this Version leftString, Version rightString) { return leftString.CompareTo(rightString) < 0; } public static string GetMostRecentlyInstalledPath(string mostRecentlyInstalledSubKeyName) { var log = new System.Diagnostics.EventLog(); log.Source = "DataPROInstaller"; var mostRecentlyInstalledPath = string.Empty; var rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); //Look first in Products var sk1 = rk.OpenSubKey(Settings.Default.RegistrySoftwareInstalledProducts); if (sk1 != null) { log.WriteEntry("Looking in Products"); foreach (var productSubKeyName in sk1.GetSubKeyNames()) { if (productSubKeyName != mostRecentlyInstalledSubKeyName) continue; var newKey = sk1.OpenSubKey(productSubKeyName); if (newKey == null) continue; newKey = newKey.OpenSubKey(Settings.Default.RegistryInstallProperties); if (newKey == null) continue; var val = newKey.GetValue(Settings.Default.RegistryInstallLocation, -1, RegistryValueOptions.None).ToString(); if ((val == "-1") || (string.IsNullOrWhiteSpace(val))) continue; mostRecentlyInstalledPath = val; if (mostRecentlyInstalledPath.Contains(Settings.Default.DTSSuite)) { mostRecentlyInstalledPath += Settings.Default.RegistryDataPRO + "\\"; } log.WriteEntry("GetMostRecentlyInstalledPath returned " + mostRecentlyInstalledPath + " from Products"); return mostRecentlyInstalledPath; } } //Look in Components log.WriteEntry("Looking in Components"); sk1 = rk.OpenSubKey(Settings.Default.RegistrySoftwareInstalledComponents); if (sk1 == null) return string.Empty; foreach (var productSubKeyName in sk1.GetSubKeyNames()) { var newKey = sk1.OpenSubKey(productSubKeyName); if (newKey == null) continue; var val = newKey.GetValue(mostRecentlyInstalledSubKeyName, -1, RegistryValueOptions.None).ToString(); if ((val == "-1") || (!val.EndsWith(Settings.Default.RegistryDataPROExeConfig))) continue; mostRecentlyInstalledPath = val.Substring(0, val.Length - Settings.Default.RegistryDataPROExeConfig.Length); if (mostRecentlyInstalledPath.Contains(Settings.Default.DTSSuite)) { mostRecentlyInstalledPath += Settings.Default.RegistryDataPRO + "\\"; } log.WriteEntry("GetMostRecentlyInstalledPath returned " + mostRecentlyInstalledPath + " from Components"); return mostRecentlyInstalledPath; } log.WriteEntry("GetMostRecentlyInstalledPath returned " + mostRecentlyInstalledPath); return mostRecentlyInstalledPath; } } }