public static bool RenameSubKeySafe(this RegistryKey key, string oldName, string newName) { try { key.CopyKey(oldName, newName); key.DeleteSubKeyTree(oldName); return(true); } catch { key.DeleteSubKeyTreeSafe(newName); return(false); } }
/* * Derived and Adapted from drdandle's article, * Copy and Rename Registry Keys at Code project. * Copy and Rename Registry Keys (Post Date: November 11, 2006) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * This is a work that is not of the original. It * has been modified to suit the needs of another * application. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * First Modified by StingRaptor on January 21, 2016 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Original Source: * http://www.codeproject.com/Articles/16343/Copy-and-Rename-Registry-Keys */ /// <summary> /// Attempts to rename a sub-key to the key provided using the specified old /// name and new name. /// </summary> /// <param name="key">The key of which the subkey is to be renamed from.</param> /// <param name="oldName">The old name of the sub-key.</param> /// <param name="newName">The new name of the sub-key.</param> /// <returns>Returns boolean value if the action succeded or failed; Returns /// </returns> public static bool RenameSubKeySafe(this RegistryKey key, string oldName, string newName) { try { //Copy from old to new key.CopyKey(oldName, newName); //Despose of the old key key.DeleteSubKeyTree(oldName); return(true); } catch { //Try to despose of the newKey (The rename failed) key.DeleteSubKeyTreeSafe(newName); return(false); } }
/// <summary> /// Attempts to delete the desired sub-key from the specified parent. /// </summary> /// <param name="name">The name of the sub-key to delete.</param> /// <param name="parentPath">The path to the parent for which to delete the sub-key on.</param> /// <param name="errorMsg">output parameter that contians possible error message.</param> /// <returns>Returns true if the operation succeeded.</returns> public static bool DeleteRegistryKey(string name, string parentPath, out string errorMsg) { try { RegistryKey parent = GetWritableRegistryKey(parentPath); //Invalid can not open parent if (parent == null) { errorMsg = "You do not have write access to registry: " + parentPath + ", try running client as administrator"; return(false); } //Child does not exist if (!parent.ContainsSubKey(name)) { errorMsg = "The registry: " + name + " does not exist in: " + parentPath; //If child does not exists then the action has already succeeded return(true); } bool success = parent.DeleteSubKeyTreeSafe(name); //Child could not be deleted if (!success) { errorMsg = REGISTRY_KEY_DELETE_ERROR; return(false); } //Child was successfully deleted errorMsg = ""; return(true); } catch (Exception ex) { errorMsg = ex.Message; return(false); } }
public static bool DeleteRegistryKey(string name, string parentPath, out string errorMsg) { try { RegistryKey parent = GetWritableRegistryKey(parentPath); if (parent == null) { errorMsg = "Bu kayıdı açmak için izniniz yoktur: " + parentPath + ", clientin yönetici olarak çalıştırılması gerekiyor."; return(false); } if (!parent.ContainsSubKey(name)) { errorMsg = "Bu kayıt: " + name + " bu dizinde bulunmamaktadır: " + parentPath; return(true); } var success = parent.DeleteSubKeyTreeSafe(name); if (!success) { errorMsg = REGISTRY_KEY_DELETE_ERROR; return(false); } errorMsg = ""; return(true); } catch (Exception ex) { errorMsg = ex.Message; return(false); } }