/// <summary> /// Convenient static helper method on RegistryKeyWrapper, for when someone is only intersted in knowing /// whether a particular registry key exists or not. /// </summary> public static bool KeyExists(string registryKeyPath, RegistryHive registryHive, RegistryView registryView) { using (RegistryKeyWrapper wrapper = new RegistryKeyWrapper(registryKeyPath, registryHive, registryView)) { return(wrapper.Exists()); } }
/// <summary> /// Returns the RegistryKeyWrapper around the sub key with name "name". If that does /// not exist, returns a RegistryKeyWrapper around null. /// </summary> /// <param name="name"></param> /// <returns></returns> public virtual RegistryKeyWrapper OpenSubKey(string name) { ErrorUtilities.VerifyThrowArgumentLength(name, "name"); RegistryKeyWrapper wrapper = this; string[] keyNames = name.Split(MSBuildConstants.BackslashChar, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < keyNames.Length && wrapper.Exists(); ++i) { try { wrapper = new RegistryKeyWrapper(wrapper.WrappedKey.OpenSubKey(keyNames[i], false /* not writeable */), _registryHive); } catch (Exception ex) { if (ExceptionHandling.NotExpectedRegistryException(ex)) { throw; } throw new RegistryException(ex.Message, wrapper.Name + "\\" + keyNames[i], ex); } } return(wrapper); }
/// <summary> /// Reads a string value from the specified registry key /// </summary> /// <param name="wrapper">wrapper around key</param> /// <param name="valueName">name of the value</param> /// <returns>string data in the value</returns> private static string GetValue(RegistryKeyWrapper wrapper, string valueName) { if (wrapper.Exists()) { object result = wrapper.GetValue(valueName); // RegistryKey.GetValue returns null if the value is not present // and String.Empty if the value is present and no data is defined. // We preserve this distinction, because a string property in the registry with // no value really has an empty string for a value (which is a valid property value) // rather than null for a value (which is an invalid property value) if (result != null) { // Must be a value of string type if (!(result is string)) { InvalidToolsetDefinitionException.Throw("NonStringDataInRegistry", wrapper.Name + "@" + valueName); } return(result.ToString()); } } return(null); }