Advertisement
LaPanthere

Registry Class

Nov 6th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Win32;
  6.  
  7. namespace ...
  8. {
  9.     class RegistryClass
  10.     {
  11.         public static bool AddRegistryKey(string Location, string Name, string Value, RegistryValueKind regValueKind = RegistryValueKind.String)
  12.         {
  13.             try
  14.             {
  15.                 Registry.SetValue(Location, Name, Value, regValueKind);
  16.             }
  17.             catch
  18.             {
  19.                 return false;
  20.             }
  21.             return true;
  22.         }
  23.  
  24.         public static bool AddRegistryKey(RegistryHive Hive, string HivePath, string Name, string Value, RegistryValueKind regValueKind = RegistryValueKind.String)
  25.         {
  26.             try
  27.             {
  28.                 string hiveStr = "";
  29.                 switch (Hive)
  30.                 {
  31.                     case RegistryHive.CurrentUser:
  32.                         hiveStr = @"HKEY_CURRENT_USER\";
  33.                         break;
  34.                     case RegistryHive.LocalMachine:
  35.                         hiveStr = @"HKEY_LOCAL_MACHINE\";
  36.                         break;
  37.                     case RegistryHive.ClassesRoot:
  38.                         hiveStr = @"HKEY_CLASSES_ROOT\";
  39.                         break;
  40.                     case RegistryHive.Users:
  41.                         hiveStr = @"HKEY_USERS\";
  42.                         break;
  43.                     case RegistryHive.CurrentConfig:
  44.                         hiveStr = @"HKEY_CURRENT_CONFIG\";
  45.                         break;
  46.                 }
  47.                 Registry.SetValue(hiveStr + HivePath, Name, Value, regValueKind);
  48.             }
  49.             catch
  50.             {
  51.                 return false;
  52.             }
  53.             return true;
  54.         }
  55.  
  56.         public static bool DeleteRegistryValue(string Location, string Name)
  57.         {
  58.             try
  59.             {
  60.                 Registry.SetValue(Location, Name, "");
  61.             }
  62.             catch
  63.             {
  64.                 return false;
  65.             }
  66.             return true;
  67.         }
  68.  
  69.         public static bool DeleteRegistryKey(RegistryHive Hive, string HivePath, string Name)
  70.         {
  71.             try
  72.             {
  73.                 switch (Hive)
  74.                 {
  75.                     case RegistryHive.CurrentUser:
  76.                         Registry.CurrentUser.OpenSubKey(HivePath, true).DeleteValue(Name);
  77.                         break;
  78.                     case RegistryHive.LocalMachine:
  79.                         Registry.LocalMachine.OpenSubKey(HivePath, true).DeleteValue(Name);
  80.                         break;
  81.                     case RegistryHive.ClassesRoot:
  82.                         Registry.ClassesRoot.OpenSubKey(HivePath, true).DeleteValue(Name);
  83.                         break;
  84.                     case RegistryHive.Users:
  85.                         Registry.Users.OpenSubKey(HivePath, true).DeleteValue(Name);
  86.                         break;
  87.                     case RegistryHive.CurrentConfig:
  88.                         Registry.CurrentConfig.OpenSubKey(HivePath, true).DeleteValue(Name);
  89.                         break;
  90.                 }
  91.             }
  92.             catch
  93.             {
  94.                 return false;
  95.             }
  96.             return true;
  97.         }
  98.  
  99.         public static bool ChangeKeyValue(string keyname, string keylocation, string newvalue, RegistryValueKind regValueKind = RegistryValueKind.String)
  100.         {
  101.             try
  102.             {
  103.                 Registry.SetValue(keylocation, keyname, newvalue, regValueKind);
  104.             }
  105.             catch
  106.             {
  107.                 return false;
  108.             }
  109.             return true;
  110.         }
  111.  
  112.         public static object GetKeyValue(string keyname, string keylocation)
  113.         {
  114.             object Value = null;
  115.             try
  116.             {
  117.                 Registry.GetValue(keylocation, keyname, null);
  118.             }
  119.             catch
  120.             {
  121.                 return null;
  122.             }
  123.             return Value;
  124.         }
  125.  
  126.         public static object GetKeyValue(RegistryHive Hive, string HivePath, string Name)
  127.         {
  128.             object Value = null;
  129.             try
  130.             {
  131.                 switch (Hive)
  132.                 {
  133.                     case RegistryHive.CurrentUser:
  134.                         Value = Registry.CurrentUser.OpenSubKey(HivePath).GetValue(Name);
  135.                         break;
  136.                     case RegistryHive.LocalMachine:
  137.                         Value = Registry.LocalMachine.OpenSubKey(HivePath).GetValue(Name);
  138.                         break;
  139.                     case RegistryHive.ClassesRoot:
  140.                         Value = Registry.ClassesRoot.OpenSubKey(HivePath).GetValue(Name);
  141.                         break;
  142.                     case RegistryHive.Users:
  143.                         Value = Registry.Users.OpenSubKey(HivePath).GetValue(Name);
  144.                         break;
  145.                     case RegistryHive.CurrentConfig:
  146.                         Value = Registry.CurrentConfig.OpenSubKey(HivePath).GetValue(Name);
  147.                         break;
  148.                 }
  149.             }
  150.             catch
  151.             {
  152.                 return null;
  153.             }
  154.             return Value;
  155.         }
  156.  
  157.         public static bool CheckKeyExists(string keyname, string keylocation)
  158.         {
  159.             try
  160.             {
  161.                 object KeyValue = Registry.GetValue(keylocation, keyname, null);
  162.                 if (KeyValue != null)
  163.                 {
  164.                     return true;
  165.                 }
  166.             }
  167.             catch
  168.             {
  169.                 return false;
  170.             }
  171.             return false;
  172.         }
  173.  
  174.         public static bool CheckKeyExists(RegistryHive Hive, string HivePath, string keyname)
  175.         {
  176.             try
  177.             {
  178.                 switch (Hive)
  179.                 {
  180.                     case RegistryHive.CurrentUser:
  181.                         object KeyValue1 = Registry.CurrentUser.OpenSubKey(HivePath).GetValue(keyname);
  182.                         if (KeyValue1 != null)
  183.                         {
  184.                             return true;
  185.                         } break;
  186.                     case RegistryHive.LocalMachine:
  187.                         object KeyValue2 = Registry.LocalMachine.OpenSubKey(HivePath).GetValue(keyname);
  188.                         if (KeyValue2 != null)
  189.                         {
  190.                             return true;
  191.                         } break;
  192.                     case RegistryHive.ClassesRoot:
  193.                         object KeyValue3 = Registry.ClassesRoot.OpenSubKey(HivePath).GetValue(keyname);
  194.                         if (KeyValue3 != null)
  195.                         {
  196.                             return true;
  197.                         } break;
  198.                     case RegistryHive.Users:
  199.                         object KeyValue4 = Registry.Users.OpenSubKey(HivePath).GetValue(keyname);
  200.                         if (KeyValue4 != null)
  201.                         {
  202.                             return true;
  203.                         } break;
  204.                     case RegistryHive.CurrentConfig:
  205.                         object KeyValue5 = Registry.CurrentConfig.OpenSubKey(HivePath).GetValue(keyname);
  206.                         if (KeyValue5 != null)
  207.                         {
  208.                             return true;
  209.                         } break;
  210.                 }
  211.             }
  212.             catch
  213.             {
  214.                 return false;
  215.             }
  216.             return false;
  217.         }
  218.  
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement