This is the class I created for my registry function, this class will test if your running a 64 bit OS and will work. It also contain a class for backing up and restore the registry.
- #region Disclaimer
- // ****************************************************************************************************
- // Assembly : MyRegistry
- // Author : Luc Joly
- // Created : 01-27-2018
- // Assembly : MyRegistry
- //
- // Last Modified By : Luc Joly
- // Last Modified On : 02-03-2018 : 10:45 PM
- // ****************************************************************************************************
- // ****************************************************************************************************
- // <copyright file="MyRegistry.cs" company="LuJoSoft">
- // Any distribution of source code without approval of LuJoSoft is prohibited.
- // Copyright © 2018. LuJoSoft. All rights reserved.
- // </copyright>
- // <summary>
- // Source Code and Executable Files can be used in commercial applications
- // Source Code and Executable Files can be redistributed; and Source Code
- // can be modified to create derivative works only with the Author's consent;
- // </summary>
- // ****************************************************************************************************
- #endregion
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using LJBaseLibrary.LJErrorLog;
- using Microsoft.Win32;
- namespace MyRegistry
- {
- /// <summary>
- /// Enumerate the different hive of the registry
- /// </summary>
- public enum EnumHive
- {
- /// <summary>
- /// The local machine
- /// </summary>
- LocalMachine,
- /// <summary>
- /// The current user
- /// </summary>
- CurrentUser,
- /// <summary>
- /// The classes root
- /// </summary>
- ClassesRoot,
- /// <summary>
- /// The current configuration
- /// </summary>
- CurrentConfig,
- /// <summary>
- /// The user
- /// </summary>
- User
- }
- /// <summary>
- /// A registry helper class.
- /// </summary>
- public class MyRegistry
- {
- /// <summary>
- /// Log error
- /// </summary>
- private static readonly ErrorLog Logger = new ErrorLog();
- #region registry normal function
- /// <summary>
- /// Get registry hive to work with.
- /// </summary>
- /// <param name="hive">The hive.</param>
- /// <returns>
- /// The <see cref="RegistryKey" />.
- /// </returns>
- internal static RegistryKey GetRegistryKey(EnumHive hive)
- {
- switch (hive)
- {
- case EnumHive.LocalMachine:
- return RegistryKey.OpenBaseKey(
- RegistryHive.LocalMachine,
- Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
- case EnumHive.CurrentUser:
- return RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
- case EnumHive.ClassesRoot:
- return RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
- case EnumHive.CurrentConfig:
- return RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Registry32);
- case EnumHive.User:
- return RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Registry32);
- default:
- return null;
- }
- }
- /// <summary>
- /// Return true if the key exist
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="hive">The registry hive to check</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool CheckRegistry(string keyPath, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- return regkey.OpenSubKey(keyPath) != null;
- }
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// Get registry value return object.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="keyName">The key name.</param>
- /// <param name="hive">The hive.</param>
- /// <returns>
- /// The <see cref="object" />.
- /// </returns>
- internal object GetRegistryValue(string keyPath, string keyName, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return null;
- using (var regkey2 = regkey.OpenSubKey(keyPath))
- {
- return regkey2?.GetValue(keyName);
- }
- }
- }
- catch (Exception ex)
- {
- MyMessage.Show(ex.Message);
- Logger.LogError(ex);
- return false;
- }
- }
- /// <summary>
- /// Write to the registry and return tue if successfull.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="hive">The hive.</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool WriteRegistry(string keyPath, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return false;
- regkey.CreateSubKey(keyPath, false);
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- }
- /// <summary>
- /// Write to the registry and return tue if successfull.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="keyName">The key name.</param>
- /// <param name="keyValue">The key value.</param>
- /// <param name="hive">The hive.</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool WriteRegistry(string keyPath, string keyName, object keyValue, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return false;
- regkey.CreateSubKey(keyPath, true);
- using (var regkey2 = regkey.OpenSubKey(keyPath, true))
- {
- if (regkey2 == null) return false;
- regkey2.SetValue(keyName, keyValue);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- }
- /// <summary>
- /// Write to the registry and return tue if successfull.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="keyName">The key name.</param>
- /// <param name="keyValue">The key value.</param>
- /// <param name="hive">The hive.</param>
- /// <param name="valueKind">The RegistryValueKind value kind.</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool WriteRegistry(string keyPath, string keyName, object keyValue, EnumHive hive, RegistryValueKind valueKind)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return false;
- regkey.CreateSubKey(keyPath, true);
- using (var regkey2 = regkey.OpenSubKey(keyPath, true))
- {
- if (regkey2 == null) return false;
- regkey2.SetValue(keyName, keyValue, valueKind);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- }
- /// <summary>
- /// The delete key registry.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="keyName">The key name to delete</param>
- /// <param name="hive">The registry hive to delete the key.</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool DeleteKeyRegistry(string keyPath, string keyName, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return false;
- using (var regkey2 = regkey.OpenSubKey(keyPath, true))
- {
- if (regkey2 == null) return false;
- regkey2.DeleteValue(keyName, true);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- }
- /// <summary>
- /// Delete registry tree.
- /// </summary>
- /// <param name="keyPath">Path of the key to check</param>
- /// <param name="keyName">Name of the key.</param>
- /// <param name="hive">The registry hive to delete the key.</param>
- /// <returns>
- /// The <see cref="bool" />.
- /// </returns>
- internal bool DeleteRegistryTree(string keyPath, string keyName, EnumHive hive)
- {
- try
- {
- using (var regkey = GetRegistryKey(hive))
- {
- if (regkey == null) return false;
- using (var regkey2 = regkey.OpenSubKey(keyPath, true))
- {
- if (regkey2 == null) return false;
- regkey2.DeleteSubKeyTree(keyName, true);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- MyMessage.Show(ex.Message);
- Logger.LogError(ex);
- return false;
- }
- }
- #endregion registry normal function
- #region Export and import registry key
- /// <summary>
- /// Gets the short name of the path.
- /// </summary>
- /// <param name="lpszLongPath">The LPSZ long path.</param>
- /// <param name="lpszShortPath">The LPSZ short path.</param>
- /// <param name="cchBuffer">The CCH buffer.</param>
- /// <returns></returns>
- [DllImport("kernel32", EntryPoint = "GetShortPathNameA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- private static extern int GetShortPathName([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
- /// <summary>
- /// Exports the key.
- /// </summary>
- /// <param name="sRegKeyPath">The s reg key path.</param>
- /// <param name="sfile">The sfile.</param>
- public void ExportKey(string sRegKeyPath, string sfile)
- {
- var text1 = "\"" + sRegKeyPath + "\"";
- FileAppend(sfile, "");
- ExecuteRegistry("regedit.exe", "/E " + GetDosPath(sfile) + " " + text1, ProcessWindowStyle.Normal);
- }
- /// <summary>
- /// Files the append.Append the Path Of The Key
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="text">The text.</param>
- public static void FileAppend(string path, string text)
- {
- var writer1 = File.AppendText(path);
- writer1.Write(text);
- writer1.Close();
- }
- /// <summary>
- /// Gets the dos path. This will Return the Dos Command Path
- /// </summary>
- /// <param name="path">The path.</param>
- /// <returns></returns>
- public static string GetDosPath(string path)
- {
- return GetShortFileName(path);
- }
- public static string GetShortFileName(string path)
- {
- var builder1 = new StringBuilder(0x400);
- var num1 = GetShortPathName(ref path, builder1, builder1.Capacity);
- return builder1.ToString(0, num1);
- }
- /// <summary>
- /// Executes the backup of the registry.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="arguments">The arguments.</param>
- /// <param name="style">The style.</param>
- /// <returns></returns>
- public static bool ExecuteRegistry(string path, string arguments, ProcessWindowStyle style)
- {
- var proc = new Process();
- try
- {
- proc.StartInfo.FileName = path;
- proc.StartInfo.UseShellExecute = false;
- proc = Process.Start(path, arguments);
- proc?.WaitForExit();
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- finally
- {
- proc?.Dispose();
- }
- }
- /// <summary>
- /// Restores the key to the registry.
- /// </summary>
- /// <param name="files">The files.</param>
- /// <returns></returns>
- public bool RestoreKey(string files)
- {
- var path = " /S " + "\"" + files + "\"";
- var proc = new Process();
- try
- {
- proc = Process.Start("regedit.exe", path);
- proc?.WaitForExit();
- return true;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- return false;
- }
- finally
- {
- proc?.Dispose();
- }
- }
- #endregion Export and import registry key
- }
- }
GeSHi © Codebox Plus Extension
Usage
- private readonly MyRegistry _registryHelper = new MyRegistry();
- // To use it to write
- if (_registryHelper.WriteRegistry(
- keyPath,
- keyName,
- valueToSet,
- EnumHive.CurrentUser,
- RegistryValueKind.Binary))
- // Also to write a string you don't need RegistryValueKind
- if (_registryHelper.WriteRegistry(
- keyPath,
- keyName,
- valueToSet,
- EnumHive.ClassesRoot))
- // Also to write just a key then use it like this
- if (_registryHelper.WriteRegistry(
- keyPath,
- EnumHive.ClassesRoot))
- // Check if a key exist
- if (_registryHelper.CheckRegistry(keyPath, EnumHive.ClassesRoot))
- // To delete a key tree
- if (_registryHelper.DeleteRegistryTree(keyPath, keyName, EnumHive.ClassesRoot))
- // To delete a key
- if (_registryHelper.DeleteKeyRegistry(keyPath, keyName, EnumHive.CurrentUser))
- // Get value of key
- var valu = _registryHelper.GetRegistryValue(keyPath, keyName, EnumHive.LocalMachine);
- // To backup branch of the registry
- var backup = Path.Combine(folder, "Part3.reg");
- _registryHelper.ExportKey("HKEY_CURRENT_USER", backup);
- // To backup branch of the registry
- _registryHelper.RestoreKey(file.reg) // Must be the full path
GeSHi © Codebox Plus Extension
- MyRegistry.cs
- MyRegistry
- (14.6 KiB) Downloaded 1054 times
- MyRegistry.cs
- MyRegistry
- (14.6 KiB) Downloaded 1054 times
Enjoy!
Superl
