public void TestMockWindowsRegistry() { var w = new WindowsRegistry(); IRegistryKey rCore = w.LocalMachine.OpenSubKey(@"SOFTWARE\R-core"); string localMachineTestReg = @" [HKEY_LOCAL_MACHINE\SOFTWARE\R-core] [HKEY_LOCAL_MACHINE\SOFTWARE\R-core\R] 'InstallPath'='C:\Program Files\R\R-3.3.3' 'Current Version'='3.3.3' "; var reg = new MockRegistry(localMachineTestReg); var lm = reg.LocalMachine; //var sk = lm.GetSubKeyNames(); rCore = lm.OpenSubKey(@"SOFTWARE\R-core"); var valNames = rCore.GetValueNames(); Assert.AreEqual(valNames.Length, 0); Assert.AreEqual(rCore.GetSubKeyNames().Length, 1); Assert.AreEqual(rCore.GetSubKeyNames()[0], "R"); var R = rCore.OpenSubKey(@"R"); Assert.AreEqual(R.GetSubKeyNames().Length, 0); Assert.AreEqual(R.GetValueNames().Length, 2); Assert.AreEqual(R.GetValue("InstallPath"), "C:\\Program Files\\R\\R-3.3.3"); Assert.AreEqual(R.GetValue("Current Version"), "3.3.3"); localMachineTestReg = @" [HKEY_LOCAL_MACHINE\SOFTWARE\R-core\R\R64] 'InstallPath'='C:\Program Files\Microsoft\R Client\R_SERVER\' 'Current Version'='3.2.2.803' [HKEY_LOCAL_MACHINE\SOFTWARE\R-core\R\R64\3.2.2.803] 'InstallPath'='C:\Program Files\Microsoft\R Client\R_SERVER\' "; reg = new MockRegistry(localMachineTestReg); lm = reg.LocalMachine; rCore = lm.OpenSubKey(@"SOFTWARE\R-core"); Assert.AreEqual(rCore.GetValueNames().Length, 0); Assert.AreEqual(rCore.GetSubKeyNames().Length, 1); Assert.AreEqual(rCore.GetSubKeyNames()[0], "R"); R = rCore.OpenSubKey(@"R"); Assert.AreEqual(R.GetSubKeyNames().Length, 1); var R64 = lm.OpenSubKey(@"SOFTWARE\R-core\R\R64"); Assert.AreEqual(R64.GetSubKeyNames().Length, 1); Assert.AreEqual(R64.GetValueNames().Length, 2); Assert.AreEqual(R64.GetValue("InstallPath"), @"C:\Program Files\Microsoft\R Client\R_SERVER\"); Assert.AreEqual(R64.GetValue("Current Version"), "3.2.2.803"); }
private static string TryFindInstallationPath(string msiDisplayName, string applicationExeName, IRegistry registry, IFile file) { foreach (string key in SoftwareKeys) { using (IRegistryKey rk = registry.LocalMachine.OpenSubKey(key)) { if (rk == null) { continue; } //Let's go through the registry keys and get the info we need: foreach (string skName in rk.GetSubKeyNames()) { using (IRegistryKey sk = rk.OpenSubKey(skName)) { if (sk == null) { continue; } try { //first look for PDF Architect 3 //If the key has value, continue, if not, skip it: string displayName = sk.GetValue("DisplayName").ToString(); if ((displayName.StartsWith(msiDisplayName, StringComparison.OrdinalIgnoreCase)) && !displayName.Contains("Enterprise") && (sk.GetValue("Publisher").ToString().Contains("pdfforge")) && (sk.GetValue("InstallLocation") != null)) { var installLocation = sk.GetValue("InstallLocation").ToString(); var exePath = Path.Combine(installLocation, applicationExeName); if (file.Exists(exePath)) { return(exePath); } // if the exe does not exist, this is the wrong path return(null); } } catch (Exception) { // ignored } } } } } return(null); }
private string RecurseFirstSubkey(IRegistryKey rCoreKey, StringBuilder logger) { string[] subKeyNames = rCoreKey.GetSubKeyNames(); if (subKeyNames.Length > 0) { var versionNum = subKeyNames.First(); var rVersionCoreKey = rCoreKey.OpenSubKey(versionNum); doLogSetEnvVarInfo("As a last resort, trying to recurse into " + rVersionCoreKey, logger); return(GetRInstallPathFromRCoreKegKey(rVersionCoreKey, logger)); } else { doLogSetEnvVarWarn("No sub-key found under " + rCoreKey, logger); return(null); } }
private string GetRInstallPathFromRCoreKegKey(IRegistryKey rCoreKey, StringBuilder logger) { string installPath = null; string[] subKeyNames = rCoreKey.GetSubKeyNames(); string[] valueNames = rCoreKey.GetValueNames(); if (valueNames.Length == 0) { doLogSetEnvVarWarn("Did not find any value names under " + rCoreKey, logger); return(RecurseFirstSubkey(rCoreKey, logger)); } else { const string installPathKey = "InstallPath"; if (valueNames.Contains(installPathKey)) { doLogSetEnvVarInfo("Found sub-key InstallPath under " + rCoreKey, logger); installPath = (string)rCoreKey.GetValue(installPathKey); } else { doLogSetEnvVarInfo("Did not find sub-key InstallPath under " + rCoreKey, logger); if (valueNames.Contains("Current Version")) { doLogSetEnvVarInfo("Found sub-key Current Version under " + rCoreKey, logger); string currentVersion = GetRCurrentVersionStringFromRegistry(rCoreKey); if (subKeyNames.Contains(currentVersion)) { IRegistryKey rVersionCoreKey = rCoreKey.OpenSubKey(currentVersion); return(GetRInstallPathFromRCoreKegKey(rVersionCoreKey, logger)); } else { doLogSetEnvVarWarn("Sub key " + currentVersion + " not found in " + rCoreKey, logger); } } else { doLogSetEnvVarInfo("Did not find sub-key Current Version under " + rCoreKey, logger); return(RecurseFirstSubkey(rCoreKey, logger)); } } } doLogSetEnvVarInfo(string.Format("InstallPath value of key " + rCoreKey.ToString() + ": {0}", installPath == null ? "null" : installPath), logger); return(installPath); }
/// <summary> /// Prints a registry key with the given depth. /// </summary> /// <param name="key">The key.</param> /// <param name="depth">The depth.</param> /// <returns>The key, printed at the given depth.</returns> private static string PrintKey(IRegistryKey key, int depth) { var indent = new string(' ', depth *3); // Get the value strings. var values = key.GetValueNames() .Select(v => $"{indent}{(string.IsNullOrEmpty(v) ? "(Default)" : v)} = {key.GetValue(v)}") .OrderBy(s => s); // Get the subkey strings. var subKeys = key.GetSubKeyNames() .OrderBy(sk => sk) .Select(sk => $"{indent}{sk}{Environment.NewLine}{PrintKey(key.OpenSubKey(sk), depth + 1)}"); return(string.Join(Environment.NewLine, values.Concat(subKeys))); }
public MareRegHandlerTests() { RegKeyFake = A.Fake <IRegistryKey>(); A.CallTo(() => RegKeyFake.GetSubKeyNames()).Returns(new string[] { "1" }); A.CallTo(() => RegKeyFake.GetValue("KMD_EnableInternalLargePage", null)).Returns(0); A.CallTo(() => RegKeyFake.GetValue("EnableCrossFireAutoLink", null)).Returns(0); A.CallTo(() => RegKeyFake.GetValue("EnableUlps", null)).Returns(0); A.CallTo(() => RegKeyFake.GetValue("DriverDesc", null)).Returns("FAKE_GPU"); A.CallTo(() => RegKeyFake.SetValue(A <string> .Ignored, A <object> .Ignored, A <RegistryValueKind> .Ignored)).DoesNothing(); A.CallTo(() => RegKeyFake.OpenSubKey(A <string> .Ignored, A <bool> .Ignored)).Returns(RegKeyFake); RegFake = A.Fake <IRegistry>(); A.CallTo(() => RegFake.OpenSubKey(A <string> .Ignored)).Returns(RegKeyFake); MsgBoxFake = A.Fake <IMessageBox>(); A.CallTo(() => MsgBoxFake.Show(A <string> .Ignored)).DoesNothing(); }
private void AddArchitectVersionKeys(IRegistryKey softwareKey, IRegistryKey softwareWow64Key) { var subkeys = new List <string>(); var subkeysWow64 = new List <string>(); foreach (var pdfArchitectVersion in _pdfArchitectVersions) { var versionKey = Substitute.For <IRegistryKey>(); versionKey.GetValue("DisplayName").Returns(pdfArchitectVersion.DisplayName); versionKey.GetValue("InstallLocation").Returns(pdfArchitectVersion.InstallLocation); versionKey.GetValue("Publisher").Returns("pdfforge"); var version = pdfArchitectVersion; IRegistryKey mockKey; IList <string> subkeyList; if (pdfArchitectVersion.IsWow64) { mockKey = softwareWow64Key; subkeyList = subkeysWow64; } else { mockKey = softwareKey; subkeyList = subkeys; } subkeyList.Add(pdfArchitectVersion.SubkeyName); if (pdfArchitectVersion.ThrowsException) { mockKey.OpenSubKey(version.SubkeyName).Returns(x => throw new IOException()); } else { mockKey.OpenSubKey(version.SubkeyName).Returns(versionKey); } } softwareKey.GetSubKeyNames().Returns(subkeys.ToArray()); softwareWow64Key.GetSubKeyNames().Returns(subkeysWow64.ToArray()); }
private void RecurseCopyKey(IRegistryKey sourceKey, IRegistryKey destinationKey) { //copy all the values foreach (var valueName in sourceKey.GetValueNames()) { var objValue = sourceKey.GetValue(valueName); var valKind = sourceKey.GetValueKind(valueName); destinationKey.SetValue(valueName, objValue, valKind); } //For Each subKey //Create a new subKey in destinationKey //Call myself foreach (var sourceSubKeyName in sourceKey.GetSubKeyNames()) { var sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName); var destSubKey = destinationKey.CreateSubKey(sourceSubKeyName); RecurseCopyKey(sourceSubKey, destSubKey); } }
private IList <GhostscriptVersion> FindRegistryInstances() { var versions = new List <GhostscriptVersion>(); IRegistryKey gsMainKey = _registry.LocalMachine.OpenSubKey(RegistryPath); if (gsMainKey == null) { return(versions); } foreach (string subkey in gsMainKey.GetSubKeyNames()) { GhostscriptVersion v = IsGhostscriptInstalled(subkey); if (v != null) { versions.Add(v); } } return(versions); }
/// <summary> /// Tries to obtaining the path to the latest version of the SQL LocalDB /// native API DLL for the currently executing process. /// </summary> /// <param name="fileName"> /// When the method returns, contains the path to the SQL Local DB API /// to use, if found; otherwise <see langword="null"/>. /// </param> /// <returns> /// <see langword="true"/> if the native API path was successfully found; /// otherwise <see langword="false"/>. /// </returns> internal bool TryGetLocalDbApiPath(out string fileName) { fileName = null; string keyName = DeriveLocalDbRegistryKey(); IRegistryKey key = Registry.OpenSubKey(keyName); if (key == null) { Logger.RegistryKeyNotFound(keyName); return(false); } Version latestVersion = null; Version overrideVersion = null; string path = null; try { // Is there a setting overriding the version to load? string overrideVersionString = ApiVersion; foreach (string versionString in key.GetSubKeyNames()) { if (!Version.TryParse(versionString, out Version version)) { Logger.InvalidRegistryKey(versionString); continue; } if (!string.IsNullOrEmpty(overrideVersionString) && overrideVersion == null && string.Equals(versionString, overrideVersionString, StringComparison.OrdinalIgnoreCase)) { Logger.NativeApiVersionOverriddenByUser(version); overrideVersion = version; } if (latestVersion == null || latestVersion < version) { latestVersion = version; } } if (!string.IsNullOrEmpty(overrideVersionString) && overrideVersion == null) { Logger.NativeApiVersionOverrideNotFound(overrideVersionString); } Version versionToUse = overrideVersion ?? latestVersion; if (versionToUse != null) { using (IRegistryKey subkey = key.OpenSubKey(versionToUse.ToString())) { path = subkey.GetValue("InstanceAPIPath"); } NativeApiVersion = versionToUse; } } finally { key.Dispose(); } if (string.IsNullOrEmpty(path)) { Logger.NativeApiNotFound(); return(false); } if (!File.Exists(path)) { Logger.NativeApiLibraryNotFound(path); return(false); } fileName = Path.GetFullPath(path); return(true); }
/// <summary> /// Tries to obtaining the path to the latest version of the SQL LocalDB /// native API DLL for the currently executing process. /// </summary> /// <param name="fileName"> /// When the method returns, contains the path to the SQL Local DB API /// to use, if found; otherwise <see langword="null"/>. /// </param> /// <returns> /// <see langword="true"/> if the native API path was successfully found; /// otherwise <see langword="false"/>. /// </returns> internal static bool TryGetLocalDbApiPath(out string fileName) { fileName = null; bool isWow64Process = Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess; // Open the appropriate Registry key if running as a 32-bit process on a 64-bit machine string keyName = string.Format( CultureInfo.InvariantCulture, @"SOFTWARE\{0}Microsoft\Microsoft SQL Server Local DB\Installed Versions", isWow64Process ? @"Wow6432Node\" : string.Empty); IRegistryKey key = Registry.OpenSubKey(keyName); if (key == null) { Logger.Warning(Logger.TraceEvent.RegistryKeyNotFound, SR.NativeMethods_RegistryKeyNotFoundFormat, keyName); return(false); } Version latestVersion = null; Version overrideVersion = null; string path = null; try { // Is there a setting overriding the version to load? string overrideVersionString = SqlLocalDbConfig.NativeApiOverrideVersionString; foreach (string versionString in key.GetSubKeyNames()) { Version version; try { version = new Version(versionString); } catch (ArgumentException) { Logger.Warning(Logger.TraceEvent.InvalidRegistryKey, SR.NativeMethods_InvalidRegistryKeyNameFormat, versionString); continue; } catch (FormatException) { Logger.Warning(Logger.TraceEvent.InvalidRegistryKey, SR.NativeMethods_InvalidRegistryKeyNameFormat, versionString); continue; } catch (OverflowException) { Logger.Warning(Logger.TraceEvent.InvalidRegistryKey, SR.NativeMethods_InvalidRegistryKeyNameFormat, versionString); continue; } if (!string.IsNullOrEmpty(overrideVersionString) && overrideVersion == null && string.Equals(versionString, overrideVersionString, StringComparison.OrdinalIgnoreCase)) { Logger.Verbose(Logger.TraceEvent.NativeApiVersionOverriddenByUser, SR.NativeMethods_ApiVersionOverriddenByUserFormat, version); overrideVersion = version; } if (latestVersion == null || latestVersion < version) { latestVersion = version; } } if (!string.IsNullOrEmpty(overrideVersionString) && overrideVersion == null) { Logger.Warning( Logger.TraceEvent.NativeApiVersionOverrideNotFound, SR.NativeMethods_OverrideVersionNotFoundFormat, overrideVersionString, Environment.MachineName, latestVersion); } Version versionToUse = overrideVersion ?? latestVersion; if (versionToUse != null) { using (var subkey = key.OpenSubKey(versionToUse.ToString())) { path = subkey.GetValue("InstanceAPIPath"); } NativeApiVersion = versionToUse; } } finally { key.Dispose(); } if (string.IsNullOrEmpty(path)) { Logger.Warning(Logger.TraceEvent.NoNativeApiFound, SR.NativeMethods_NoNativeApiFound); return(false); } if (!File.Exists(path)) { Logger.Error(Logger.TraceEvent.NativeApiPathNotFound, SR.NativeMethods_NativeApiNotFoundFormat, path); return(false); } fileName = Path.GetFullPath(path); return(true); }