public static RegistryKey GetRootKey(RegistryRootEnum Root) { switch (Root) { case RegistryRootEnum.HKEY_CLASSES_ROOT: return(Registry.ClassesRoot); case RegistryRootEnum.HKEY_CURRENT_USER: return(Registry.CurrentUser); case RegistryRootEnum.HKEY_LOCAL_MACHINE: return(Registry.LocalMachine); case RegistryRootEnum.HKEY_USERS: return(Registry.Users); case RegistryRootEnum.HKEY_CURRENT_CONFIG: return(Registry.CurrentConfig); case RegistryRootEnum.HKEY_DYN_DATA: return(Registry.DynData); case RegistryRootEnum.HKEY_PERFORMENCE_DATA: return(Registry.PerformanceData); } return(null); }
public static bool KeyExist(RegistryRootEnum root, string path, string folder, string key) { bool flag = false; RegistryKey rootKey = GetRootKey(root); if (rootKey == null) { throw new Exception("Registry is not exist!"); } if ((path == string.Empty) && (folder == string.Empty)) { throw new Exception("Path or folder is not exist!"); } if (key != String.Empty) { rootKey = rootKey.OpenSubKey(path, true); if (rootKey == null) { return(false); } flag = rootKey.GetValueNames().ToList().Exists(obj => obj == key); rootKey.Close(); rootKey = null; return(flag); } return(true); }
public static void SetRegistryValue(RegistryRootEnum Root, string Path, string Key, string Value) { RegistryKey rootKey = GetRootKey(Root); if ((rootKey == null) || (Path == string.Empty)) { return; } rootKey = rootKey.OpenSubKey(Path, true); rootKey.SetValue(Key, Value); rootKey.Close(); rootKey = null; }
public static List <string> GetSettingNames(RegistryRootEnum root, string path) { string[] valueNames = null; RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, String.Empty, String.Empty)) { rootKey = rootKey.OpenSubKey(path, true); valueNames = rootKey.GetValueNames(); KeyClose(rootKey); } return(valueNames.ToList()); }
public static int GetRegistryKeyValueCount(RegistryRootEnum root, string path) { int valueCount = 0; RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, String.Empty, String.Empty)) { rootKey = rootKey.OpenSubKey(path, false); valueCount = rootKey.ValueCount; KeyClose(rootKey); } return(valueCount); }
public static string GetRegistryKeyValue(RegistryRootEnum root, string path, string key) { string value = String.Empty; RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, String.Empty, key)) { rootKey = rootKey.OpenSubKey(path, true); value = rootKey.GetValue(key, String.Empty).ToString(); KeyClose(rootKey); } return(value); }
public static List <string> GetRegistryFolderNames(RegistryRootEnum root, string path) { string[] subKeyNames = null; RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, String.Empty, String.Empty)) { rootKey = rootKey.OpenSubKey(path, false); subKeyNames = rootKey.GetSubKeyNames(); KeyClose(rootKey); } return(subKeyNames.ToList()); }
public static void DeleteKeyValue(RegistryRootEnum root, string path, string key) { RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, String.Empty, key)) { try { rootKey = rootKey.OpenSubKey(path, true); rootKey.DeleteValue(key, false); KeyClose(rootKey); } catch (Exception ex) { throw ex; } } }
public static void DeleteRegistryFolder(RegistryRootEnum root, string path, string folder) { RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, folder, String.Empty)) { try { rootKey = rootKey.OpenSubKey(path, true); rootKey.DeleteSubKey(folder, false); KeyClose(rootKey); } catch (Exception ex) { throw ex; } } }
public static void CreateRegistryFolder(RegistryRootEnum root, string path, string folder) { RegistryKey rootKey = GetRootKey(root); if (KeyExist(root, path, folder, String.Empty)) { try { rootKey = rootKey.OpenSubKey(path, true); rootKey.CreateSubKey(folder).Close(); KeyClose(rootKey); } catch (System.Security.SecurityException ex) { throw ex; } catch (UnauthorizedAccessException ex) { throw ex; } } }