Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Win32;
- namespace ...
- {
- class RegistryClass
- {
- public static bool AddRegistryKey(string Location, string Name, string Value, RegistryValueKind regValueKind = RegistryValueKind.String)
- {
- try
- {
- Registry.SetValue(Location, Name, Value, regValueKind);
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static bool AddRegistryKey(RegistryHive Hive, string HivePath, string Name, string Value, RegistryValueKind regValueKind = RegistryValueKind.String)
- {
- try
- {
- string hiveStr = "";
- switch (Hive)
- {
- case RegistryHive.CurrentUser:
- hiveStr = @"HKEY_CURRENT_USER\";
- break;
- case RegistryHive.LocalMachine:
- hiveStr = @"HKEY_LOCAL_MACHINE\";
- break;
- case RegistryHive.ClassesRoot:
- hiveStr = @"HKEY_CLASSES_ROOT\";
- break;
- case RegistryHive.Users:
- hiveStr = @"HKEY_USERS\";
- break;
- case RegistryHive.CurrentConfig:
- hiveStr = @"HKEY_CURRENT_CONFIG\";
- break;
- }
- Registry.SetValue(hiveStr + HivePath, Name, Value, regValueKind);
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static bool DeleteRegistryValue(string Location, string Name)
- {
- try
- {
- Registry.SetValue(Location, Name, "");
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static bool DeleteRegistryKey(RegistryHive Hive, string HivePath, string Name)
- {
- try
- {
- switch (Hive)
- {
- case RegistryHive.CurrentUser:
- Registry.CurrentUser.OpenSubKey(HivePath, true).DeleteValue(Name);
- break;
- case RegistryHive.LocalMachine:
- Registry.LocalMachine.OpenSubKey(HivePath, true).DeleteValue(Name);
- break;
- case RegistryHive.ClassesRoot:
- Registry.ClassesRoot.OpenSubKey(HivePath, true).DeleteValue(Name);
- break;
- case RegistryHive.Users:
- Registry.Users.OpenSubKey(HivePath, true).DeleteValue(Name);
- break;
- case RegistryHive.CurrentConfig:
- Registry.CurrentConfig.OpenSubKey(HivePath, true).DeleteValue(Name);
- break;
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static bool ChangeKeyValue(string keyname, string keylocation, string newvalue, RegistryValueKind regValueKind = RegistryValueKind.String)
- {
- try
- {
- Registry.SetValue(keylocation, keyname, newvalue, regValueKind);
- }
- catch
- {
- return false;
- }
- return true;
- }
- public static object GetKeyValue(string keyname, string keylocation)
- {
- object Value = null;
- try
- {
- Registry.GetValue(keylocation, keyname, null);
- }
- catch
- {
- return null;
- }
- return Value;
- }
- public static object GetKeyValue(RegistryHive Hive, string HivePath, string Name)
- {
- object Value = null;
- try
- {
- switch (Hive)
- {
- case RegistryHive.CurrentUser:
- Value = Registry.CurrentUser.OpenSubKey(HivePath).GetValue(Name);
- break;
- case RegistryHive.LocalMachine:
- Value = Registry.LocalMachine.OpenSubKey(HivePath).GetValue(Name);
- break;
- case RegistryHive.ClassesRoot:
- Value = Registry.ClassesRoot.OpenSubKey(HivePath).GetValue(Name);
- break;
- case RegistryHive.Users:
- Value = Registry.Users.OpenSubKey(HivePath).GetValue(Name);
- break;
- case RegistryHive.CurrentConfig:
- Value = Registry.CurrentConfig.OpenSubKey(HivePath).GetValue(Name);
- break;
- }
- }
- catch
- {
- return null;
- }
- return Value;
- }
- public static bool CheckKeyExists(string keyname, string keylocation)
- {
- try
- {
- object KeyValue = Registry.GetValue(keylocation, keyname, null);
- if (KeyValue != null)
- {
- return true;
- }
- }
- catch
- {
- return false;
- }
- return false;
- }
- public static bool CheckKeyExists(RegistryHive Hive, string HivePath, string keyname)
- {
- try
- {
- switch (Hive)
- {
- case RegistryHive.CurrentUser:
- object KeyValue1 = Registry.CurrentUser.OpenSubKey(HivePath).GetValue(keyname);
- if (KeyValue1 != null)
- {
- return true;
- } break;
- case RegistryHive.LocalMachine:
- object KeyValue2 = Registry.LocalMachine.OpenSubKey(HivePath).GetValue(keyname);
- if (KeyValue2 != null)
- {
- return true;
- } break;
- case RegistryHive.ClassesRoot:
- object KeyValue3 = Registry.ClassesRoot.OpenSubKey(HivePath).GetValue(keyname);
- if (KeyValue3 != null)
- {
- return true;
- } break;
- case RegistryHive.Users:
- object KeyValue4 = Registry.Users.OpenSubKey(HivePath).GetValue(keyname);
- if (KeyValue4 != null)
- {
- return true;
- } break;
- case RegistryHive.CurrentConfig:
- object KeyValue5 = Registry.CurrentConfig.OpenSubKey(HivePath).GetValue(keyname);
- if (KeyValue5 != null)
- {
- return true;
- } break;
- }
- }
- catch
- {
- return false;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement