/// <summary> /// Check if Ghostscript is installed with a given version. It does a lookup in the registry and checks if the paths /// exist. /// </summary> /// <param name="version">Name of the version to check, i.e. "9.05"</param> /// <returns>A GhostscriptVersion object if a version has been found, null otherwise</returns> private GhostscriptVersion IsGhostscriptInstalled(string version) { try { IRegistryKey myKey = _registry.LocalMachine.OpenSubKey(RegistryPath + "\\" + version); if (myKey == null) { return(null); } var gsDll = (string)myKey.GetValue("GS_DLL"); var gsLib = (string)myKey.GetValue("GS_LIB"); var gsExe = _pathSafe.Combine(_pathSafe.GetDirectoryName(gsDll), "gswin32c.exe"); myKey.Close(); if (_file.Exists(gsExe)) { return(new GhostscriptVersion(version, gsExe, gsLib)); } } catch { return(null); } return(null); }
private string GetRegistryRepositoryPath(XElement packagesElement, IEnumerable <IRegistryKey> registryKeys) { string keyName = packagesElement.GetOptionalAttributeValue("keyName"); if (String.IsNullOrEmpty(keyName)) { ShowErrorMessage(VsResources.TemplateWizard_MissingRegistryKeyName); throw new WizardBackoutException(); } IRegistryKey repositoryKey = null; string repositoryValue = null; // When pulling the repository from the registry, use CurrentUser first, falling back onto LocalMachine registryKeys = registryKeys ?? new[] { new RegistryKeyWrapper(Microsoft.Win32.Registry.CurrentUser), new RegistryKeyWrapper(Microsoft.Win32.Registry.LocalMachine) }; // Find the first registry key that supplies the necessary subkey/value foreach (var registryKey in registryKeys) { repositoryKey = registryKey.OpenSubKey(RegistryKeyRoot); if (repositoryKey != null) { repositoryValue = repositoryKey.GetValue(keyName) as string; if (!String.IsNullOrEmpty(repositoryValue)) { break; } repositoryKey.Close(); } } if (repositoryKey == null) { ShowErrorMessage(String.Format(VsResources.TemplateWizard_RegistryKeyError, RegistryKeyRoot)); throw new WizardBackoutException(); } if (String.IsNullOrEmpty(repositoryValue)) { ShowErrorMessage(String.Format(VsResources.TemplateWizard_InvalidRegistryValue, keyName, RegistryKeyRoot)); throw new WizardBackoutException(); } // Ensure a trailing slash so that the path always gets read as a directory repositoryValue = PathUtility.EnsureTrailingSlash(repositoryValue); return(Path.GetDirectoryName(repositoryValue)); }
/// <summary> /// Gets the folder location where packages have been laid down in a registry-specified location. /// </summary> /// <param name="keyName">The registry key name that specifies the packages location.</param> /// <returns>The absolute path to the packages folder specified in the registry.</returns> private string GetRegistryRepositoryPath(string keyName) { IRegistryKey repositoryKey = null; string repositoryValue = null; // When pulling the repository from the registry, use CurrentUser first, falling back onto LocalMachine // Documented here: https://docs.microsoft.com/nuget/visual-studio-extensibility/visual-studio-templates#registry-specified-folder-path var registryKeys = new[] { new RegistryKeyWrapper(RegistryHive.CurrentUser), new RegistryKeyWrapper(RegistryHive.LocalMachine, RegistryView.Registry32) }; // Find the first registry key that supplies the necessary subkey/value foreach (var registryKey in registryKeys) { repositoryKey = registryKey.OpenSubKey(_registryKeyRoot); if (repositoryKey != null) { repositoryValue = repositoryKey.GetValue(keyName) as string; if (!string.IsNullOrEmpty(repositoryValue)) { break; } repositoryKey.Close(); } } if (repositoryKey == null) { var errorMessage = string.Format(VsResources.PreinstalledPackages_RegistryKeyError, _registryKeyRoot); _errorHandler(errorMessage); // The error is fatal, cannot continue throw new InvalidOperationException(errorMessage); } if (string.IsNullOrEmpty(repositoryValue)) { var errorMessage = string.Format(VsResources.PreinstalledPackages_InvalidRegistryValue, keyName, _registryKeyRoot); _errorHandler(errorMessage); // The error is fatal, cannot continue throw new InvalidOperationException(errorMessage); } // Ensure a trailing slash so that the path always gets read as a directory repositoryValue = PathUtility.EnsureTrailingSlash(repositoryValue); return(Path.GetDirectoryName(repositoryValue)); }
private static void ReleasePolicyRegistryKeys(IRegistryKey machineKey, IRegistryKey userKey) { if (machineKey != null) { try { machineKey.Close(); } catch (Exception) { } } if (userKey != null) { try { userKey.Close(); } catch (Exception) { } } }
/// <summary> /// Gets the folder location where packages have been laid down in a registry-specified location. /// </summary> /// <param name="keyName">The registry key name that specifies the packages location.</param> /// <param name="registryKeys">The optional list of parent registry keys to look in (used for unit tests).</param> /// <param name="throwingErrorHandler"> /// An error handler that accepts the error message string and then throws /// the appropriate exception. /// </param> /// <returns>The absolute path to the packages folder specified in the registry.</returns> internal string GetRegistryRepositoryPath(string keyName, IEnumerable <IRegistryKey> registryKeys, Action <string> throwingErrorHandler) { IRegistryKey repositoryKey = null; string repositoryValue = null; // When pulling the repository from the registry, use CurrentUser first, falling back onto LocalMachine registryKeys = registryKeys ?? new[] { new RegistryKeyWrapper(Registry.CurrentUser), new RegistryKeyWrapper(Registry.LocalMachine) }; // Find the first registry key that supplies the necessary subkey/value foreach (var registryKey in registryKeys) { repositoryKey = registryKey.OpenSubKey(RegistryKeyRoot); if (repositoryKey != null) { repositoryValue = repositoryKey.GetValue(keyName) as string; if (!String.IsNullOrEmpty(repositoryValue)) { break; } repositoryKey.Close(); } } if (repositoryKey == null) { throwingErrorHandler(String.Format(VsResources.PreinstalledPackages_RegistryKeyError, RegistryKeyRoot)); Debug.Fail("throwingErrorHandler did not throw"); } if (String.IsNullOrEmpty(repositoryValue)) { throwingErrorHandler(String.Format(VsResources.PreinstalledPackages_InvalidRegistryValue, keyName, RegistryKeyRoot)); Debug.Fail("throwingErrorHandler did not throw"); } // Ensure a trailing slash so that the path always gets read as a directory repositoryValue = PathUtility.EnsureTrailingSlash(repositoryValue); return(Path.GetDirectoryName(repositoryValue)); }
private bool TryFindInRegistry(string exeName, out string fullPath) { fullPath = null; const string keyBase = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"; string expectedKey = string.Format(@"{0}\{1}", keyBase, exeName); IRegistryKey hklm = _registry.LocalMachine; IRegistryKey fileKey = hklm.OpenSubKey(expectedKey); if (fileKey == null) { return(false); } object result; try { result = fileKey.GetValue(string.Empty); } finally { fileKey.Close(); } if (result == null) { return(false); } string suggestedPath = result.ToString(); bool exists = _fileSystem.File.Exists(suggestedPath); if (!exists) { _log.Debug( "{0} was specified in registry to be located at {1}, but it wasn't found there.", exeName, suggestedPath); return(false); } _log.Debug("Found {0} at {1} (via registry).", exeName, suggestedPath); fullPath = suggestedPath; return(true); }