1. Introduction to the registry operation
Registry class,RegistryKey classProvides an interface for operating the registry
RegistryValueKind: The data type used to specify the operation registry
Registry Nest
In the registry, the top node is the registry hive.
- HKEY_CLASSES_ROOT(HKCR) Contains details of the system file type, as well as the file types that the application can open, and it also contains registration information for all COM components.
- HKEY_CURRENT_USER(HKCU) Contains the user configuration of the machine the user is currently logged in, including desktop settings, environment variables, network and printer connections, and other variables that define the user's operating environment.
- HKEY_LOCAL_MACHINE(HKLM) is a large nest that contains information about all the software and hardware installed on the machine.
- HKEY_USERS(HKUSR)
- HKEY_CURRENT_CONFIG(HKCF) Contains information about the hardware on the machine.
Registry static class
The Registry class encapsulates seven basic primary keys of the registry:
- : Corresponding to HKEY_CLASSES_ROOT primary key
- : Corresponding to HKEY_CURRENT_USER primary key
- : Corresponding to HKEY_LOCAL_MACHINE primary key
- : Corresponding to HKEY_USER primary key
- : Corresponding to HEKY_CURRENT_CONFIG primary key
- : Corresponding to HKEY_DYN_DATA primary key
- : Corresponding to HKEY_PERFORMANCE_DATA primary key
const string KeyName = "HKEY_CURRENT_USER\\Example"; (keyName, "", 5280);//Default name(keyName, "TestLong", 1234567, ); int i = (int)(keyName, "", -1);//default valuelong l = (long)(keyName, "TestLong", );
RegistryKey class
The RegistryKey class encapsulates basic operations on the registry. Common functions including read, write, delete and other operations:
- Name: The name of the key (read-only)
- SubKeyCount: Number of subkeys of keys
- ValueCount: The number of values contained by the key
- Close(): Close key
- CreateSubKey(): Creates a subkey of the given name
- DeleteSubKey(): Delete the specified subkey
- DeleteSubKeyTree(): Recursively delete subkeys and all subkeys
- DeleteValue(): Delete a specified value from the key
- GetAccessControl(): Returns the access control table for the specified registry key
- GetSubKeyNames(): Returns an array of strings containing subkey names
- GetValue(): Returns the specified value
- GetValueKind(); returns the specified value and retrieves its registry data type
- GetValueNames(): Returns an array of strings containing all key-value names
- OpenSubKey(): Returns the RegistryKey instance reference representing the given subkey
- SetAccessControl(): Apply the access control table (ACL) to the specified registry key
- SetValue(); sets the specified value
2. Creation, opening and deletion of registry keys
1. CreateSubKey
//Create child test under SOFTWARE using CreateSubKey()RegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE\test"); (); ();
2. OpenSubKey
//Use OpenSubKey() to open the item and get the RegistryKey object. When the path does not exist, it is Null. The second parameter is true, which means it can be written, readable, and deleted; it can only be read when omitted.RegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE\test",true); (); ();
3. Delete, DeleteSubKey
//The main use of DeleteSubKey() is used to delete the test itemRegistryKey hklm = ; (@"SOFTWARE\test", true); // When true, an exception is thrown when the deleted registry does not exist; when false, no exception is thrown.();
3. Creation, opening and deletion of registry key values
1. Create, SetValue
//The main use of SetValue() is used, which means that the key value with the name Name and the value of RegistryTest is created under the test. The third parameter represents the key value type. When omitted, it defaults to strings.RegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE\test",true); ("Name", "RegistryTest", ); (); ();
2. Open, GetValue
//Mainly use GetValue() to obtain the key value named "Name"RegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE\test", true); string sValue = ("Name").ToString(); (); ();
3. Delete, DeleteValue
//The main use of DeleteValue() is used to delete the key value with the name "Name". The second parameter indicates whether an exception is thrownRegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE\test", true); ("Name", true); (); ();
4. Example:
1. Determine whether the registry key and registry key value exist
//Judge whether the registry key exists private bool IsRegistryKeyExist(string sKeyName) { string[] sKeyNameColl; RegistryKey hklm = ; RegistryKey hkSoftWare = (@"SOFTWARE"); sKeyNameColl = (); //Get all the subkeys under SOFTWARE foreach (string sName in sKeyNameColl) { if (sName == sKeyName) { (); (); return true; } } (); (); return false; } //Judge whether the key value exists private bool IsRegistryValueNameExist(string sValueName) { string[] sValueNameColl; RegistryKey hklm = ; RegistryKey hkTest = (@"SOFTWARE\test"); sValueNameColl = (); //Get the names of all key values under test foreach (string sName in sValueNameColl) { if (sName == sValueName) { (); (); return true; } } (); (); return false; }
2. Program starts the program
//Start the program automatically startsstring path = ; RegistryKey rk = ; RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run"); ("JcShutdown", path); (); (); //Close the program to startstring path = ; RegistryKey rk = ; RegistryKey rk2 = (@"Software\Microsoft\Windows\CurrentVersion\Run"); ("JcShutdown", false); (); ();
5. Open the remote registration form
RegistryKey baseKey = (,"192.168.0.2"); RegistryKey softwareKey = ("software\\test"); (); ();
This is all about this article about C# operation registry. I hope it will be helpful to everyone's learning and I hope everyone will support me more.