/// <summary> /// Attempts to create the desired value for the specified parent. /// </summary> /// <param name="keyPath">The path to the key for which to create the registry value on.</param> /// <param name="kind">The type of the registry value to create.</param> /// <param name="name">output parameter that holds the name of the registry value that was create.</param> /// <param name="errorMsg">output parameter that contians possible error message.</param> /// <returns>Returns true if the operation succeeded.</returns> public static bool CreateRegistryValue(string keyPath, RegistryValueKind kind, out string name, out string errorMsg) { name = ""; try { RegistryKey key = GetWritableRegistryKey(keyPath); //Invalid can not open key if (key == null) { errorMsg = "You do not have write access to registry: " + keyPath + ", try running client as administrator"; return(false); } //Try to find available names int i = 1; string testName = String.Format("New Value #{0}", i); while (key.ContainsValue(testName)) { i++; testName = String.Format("New Value #{0}", i); } name = testName; bool success = key.SetValueSafe(name, kind.GetDefault(), kind); //Value could not be created if (!success) { errorMsg = REGISTRY_VALUE_CREATE_ERROR; return(false); } //Value was successfully created errorMsg = ""; return(true); } catch (Exception ex) { errorMsg = ex.Message; return(false); } }
public static bool CreateRegistryValue(string keyPath, RegistryValueKind kind, out string name, out string errorMsg) { name = ""; try { RegistryKey key = GetWritableRegistryKey(keyPath); if (key == null) { errorMsg = "Bu kayıdı açmak için izniniz yoktur: " + keyPath + ", clientin yönetici olarak çalıştırılması gerekiyor."; return(false); } int i = 1; string testName = string.Format("Yeni Değer #{0}", i); while (key.ContainsValue(testName)) { i++; testName = string.Format("Yeni Değer #{0}", i); } name = testName; bool success = key.SetValueSafe(name, kind.GetDefault(), kind); if (!success) { errorMsg = REGISTRY_VALUE_CREATE_ERROR; return(false); } errorMsg = ""; return(true); } catch (Exception ex) { errorMsg = ex.Message; return(false); } }
/// <summary> /// Attempts to create the desired value for the specified parent. /// </summary> /// <param name="keyPath">The path to the key for which to create the registry value on.</param> /// <param name="kind">The type of the registry value to create.</param> /// <param name="name">output parameter that holds the name of the registry value that was create.</param> /// <param name="errorMsg">output parameter that contians possible error message.</param> /// <returns>Returns boolean value for if the operation failed or succeded.</returns> public static bool CreateRegistryValue(string keyPath, RegistryValueKind kind, out string name, out string errorMsg) { name = ""; try { RegistryKey key = GetWritableRegistryKey(keyPath); //Invalid can not open key if (key == null) { errorMsg = "You do not have access to open registry: " + keyPath + ", try running client as administrator"; return false; } //Try to find available names int i = 1; string testName = String.Format("New Value #{0}", i); while (key.ContainsValue(testName)) { i++; testName = String.Format("New Value #{0}", i); } name = testName; bool success = key.SetValueSafe(name, kind.GetDefault(), kind); //Value could not be created if (!success) { errorMsg = REGISTRY_VALUE_CREATE_ERROR; return false; } //Value was successfully created errorMsg = ""; return true; } catch (Exception ex) { errorMsg = ex.Message; return false; } }