#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 // **************************************************************************************************** // **************************************************************************************************** // // Any distribution of source code without approval of LuJoSoft is prohibited. // Copyright © 2018. LuJoSoft. All rights reserved. // // // 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; // // **************************************************************************************************** #endregion using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; using LJBaseLibrary.LJErrorLog; using Microsoft.Win32; namespace MyRegistry { /// /// Enumerate the different hive of the registry /// public enum EnumHive { /// /// The local machine /// LocalMachine, /// /// The current user /// CurrentUser, /// /// The classes root /// ClassesRoot, /// /// The current configuration /// CurrentConfig, /// /// The user /// User } /// /// A registry helper class. /// public class MyRegistry { /// /// Log error /// private static readonly ErrorLog Logger = new ErrorLog(); #region registry normal function /// /// Get registry hive to work with. /// /// The hive. /// /// The . /// 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; } } /// /// Return true if the key exist /// /// Path of the key to check /// The registry hive to check /// /// The . /// internal bool CheckRegistry(string keyPath, EnumHive hive) { try { using (var regkey = GetRegistryKey(hive)) { return regkey.OpenSubKey(keyPath) != null; } } catch { return false; } } /// /// Get registry value return object. /// /// Path of the key to check /// The key name. /// The hive. /// /// The . /// 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; } } /// /// Write to the registry and return tue if successfull. /// /// Path of the key to check /// The hive. /// /// The . /// 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; } } /// /// Write to the registry and return tue if successfull. /// /// Path of the key to check /// The key name. /// The key value. /// The hive. /// /// The . /// 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; } } /// /// Write to the registry and return tue if successfull. /// /// Path of the key to check /// The key name. /// The key value. /// The hive. /// The RegistryValueKind value kind. /// /// The . /// 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; } } /// /// The delete key registry. /// /// Path of the key to check /// The key name to delete /// The registry hive to delete the key. /// /// The . /// 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; } } /// /// Delete registry tree. /// /// Path of the key to check /// Name of the key. /// The registry hive to delete the key. /// /// The . /// 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 /// /// Gets the short name of the path. /// /// The LPSZ long path. /// The LPSZ short path. /// The CCH buffer. /// [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); /// /// Exports the key. /// /// The s reg key path. /// The sfile. public void ExportKey(string sRegKeyPath, string sfile) { var text1 = "\"" + sRegKeyPath + "\""; FileAppend(sfile, ""); ExecuteRegistry("regedit.exe", "/E " + GetDosPath(sfile) + " " + text1, ProcessWindowStyle.Normal); } /// /// Files the append.Append the Path Of The Key /// /// The path. /// The text. public static void FileAppend(string path, string text) { var writer1 = File.AppendText(path); writer1.Write(text); writer1.Close(); } /// /// Gets the dos path. This will Return the Dos Command Path /// /// The path. /// 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); } /// /// Executes the backup of the registry. /// /// The path. /// The arguments. /// The style. /// 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(); } } /// /// Restores the key to the registry. /// /// The files. /// 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 Import ExtendedProtectionPolicyTypeConverter registry key } }