It is written with reference to this article https://blog.csdn.net/younghaiqing/article/details/61918968
The following are written singletons with only basic functions
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestTwo { class MyRegistryInstance { private static MyRegistryInstance instance; public static MyRegistryInstance Instance() { if (instance == null) { instance = new MyRegistryInstance(); } return instance; } /// <summary> /// Create a registry entry /// </summary> /// <param name="itemName"> The name of the registry entry created </param> public void creatRegistryFunc( string itemName) { // Create and create registry keys mainly use the CreateSubKey() method of RegistryKey RegistryKey key = Registry.LocalMachine; // RegistryKey software = key.CreateSubKey("software\\test"); RegistryKey software = key.CreateSubKey( " software\ \ " + itemName); } /// <summary> /// Open registry key /// </summary> /// <param name="itemName"> Name of registry key to open </param> public void openRegistryFunc( string itemName) { // Opening the registry key mainly uses the OpenSubKey() method of the RegistryKey. Note that if the registry key does not exist, calling this method will throw an exception RegistryKey key = Registry.LocalMachine; // RegistryKey software = key.OpenSubKey("software\\test", true); // Note that the method is followed by Can have a boolean parameter, true means it can be written. false is read only RegistryKey software = key.OpenSubKey( " software\\ " +itemName, true ); } /// <summary> /// Delete the registry key Note that if the registry key does not exist, calling this method will throw an exception /// </summary> /// <param name="itemName"> To The name of the deleted registry item </param> public void deleteRegistryFunc( string itemName ) { // Delete RegistryKey key = Registry.LocalMachine; // key.DeleteSubKey("software\\test", true); // Note that if the registry key does not exist, calling this method will throw an exception key.DeleteSubKey( " software\\ " + itemName, true ); key.Close(); } /// <summary> /// Creation of key values (set value, modification) Operations such as creation and modification of key values /// </summary> /// <param name="itemName"> To create a key value The name of the registry key </param> /// <param name="keyName"> the name of the key-value pair to be created </param> /// <param name="keyValue"> the name of the key-value pair to be created value </param> public void creatKeyFunc( string itemName, string keyName, string keyValue) { // Creation of key value (setting value, modification) The operation of creating and modifying key value mainly uses the SetValue() method of RegistryKey RegistryKey key = Registry.LocalMachine; // RegistryKey software = key.OpenSubKey("software\\test ", true); // The item must already exist RegistryKey software = key.OpenSubKey( " software\\ " + itemName, true ); // The item must already exist // Create a key value named "test" under HKEY_LOCAL_MACHINE\SOFTWARE\test and the value is "blog garden". If the key value already exists, it will modify and replace the original key value, and if it does not exist, the key value will be created. // software.SetValue("test", "Blog Garden"); software.SetValue(keyName, keyValue); // Note: SetValue() also has a third parameter, which is mainly used to set the type of key value, such as : String, Binary, Dword, etc. ~~ Default is String. Such as: // software.SetValue("test", "0", RegistryValueKind.DWord); // binary information key.Close(); } /// <summary> /// Read key value /// </summary> /// <param name="itemName"> Name of registry key to read key value </param> /// < param name="keyName"> the name of the key-value pair to be read </param> /// <returns> the value of the key-value pair to be read </returns> public string readKenFunc( string itemName, string keyName) { string info = ""; RegistryKey key; key = Registry.LocalMachine; //RegistryKey myReg = key.OpenSubKey("software\\test"); RegistryKey myReg = key.OpenSubKey("software\\"+ itemName); // myReg = Key.OpenSubKey("software\\test",true); //info = myReg.GetValue("test").ToString(); info = myReg.GetValue(keyName).ToString(); myReg.Close(); return info; } /// <summary> /// Delete key /// </summary> /// <param name="itemName"> Name of registry key to delete key </param> /// <param name ="keyName"> The name of the key-value pair to delete </param> public void deleteKeyFunc( string itemName, string keyName) { // Delete the value of the key named test in the registry key Software\\test /* RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true); delKey.DeleteValue("test");*/ RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\"+ itemName, true); delKey.DeleteValue(keyName); delKey.Close(); } /// <summary> /// Determine whether the registry entry exists /// </summary> /// <param name="itemName"> The name of the registry entry to be judged </param> /// <returns > boo true if yes else false </returns> public bool IsRegeditItemExist( string itemName) { string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey( " SOFTWARE " ); // RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); subkeyNames = software.GetSubKeyNames(); // Get the sequence of names of all subkeys under this item, and pass it to the predetermined array /*foreach (string keyName in subkeyNames) { // Determine the name of the child item if (keyName == "test") { hkml.Close(); return true; } }*/ foreach (string _itemName in subkeyNames) { // Determine the name of the child item if (_itemName == itemName) { hkml.Close(); return true; } } hkml.Close(); return false; } /// <summary> /// Determine whether the key value exists /// </summary> /// <param name="itemName"> The name of the registry key to determine the key value </param> /// < param name="keyName"> The name of the key-value pair to be judged </param> /// <returns> boo is true, otherwise false </returns> public bool IsRegeditKeyExit( string itemName, string keyName) { string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; // RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test"); RegistryKey software = hkml.OpenSubKey( " SOFTWARE\\ " + itemName); // RegistryKey software = hkml.OpenSubKey(" SOFTWARE\\test", true); subkeyNames = software.GetValueNames(); // Get the sequence of names of all key values under this item and pass them to the predetermined array /*foreach (string keyName in subkeyNames) { if (keyName == "test") { hkml.Close(); return true; } }*/ foreach (string _keyName in subkeyNames) { if (_keyName == keyName) { hkml.Close(); return true; } } hkml.Close(); return false; } } }