C # performs registry key and key value operations

Keywords: C# Windows

preface

I believe everyone has encountered the situation of manually deleting the registry when installing Siemens PLC software.

For software development, the registry can be used to store some key information, which will not be deleted, so it can be used for authorization. Today, I share with you about the use of C# operation registry.

registry

The system registry contains a lot of system and software installation information, so if you want to operate the registry, you must understand it first, otherwise, it may lead to some unexpected problems, which is not worth the loss.

1. We can enter Regedit through CMD to open the registry.

2. The registry has several key nodes, also known as root branches, which are HKEY_CLASSES_ROOT,HKEY_CURRENT_USER,HKEY_LOCAL_MACHINE,HKEY_USERS,HKEY_CURRENT_CONFIG, each node is used to store different information.

3. After expansion, each folder icon represents a registry key.

 

4. The data corresponding to each registry key is called a key value pair.

C# operation registry key

To C# operate the registry, first reference the namespace Microsoft.Win32, and then operate the RegistryKey object under the namespace.

using Microsoft.Win32;

1. Use C# to open the registry key: open HKEY in the registry_ LOCAL_ MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion",true);

The OpenSubKey method is followed by a bool type value true, which represents a writable operation.

2. Using C# to create a registry key: in HKEY_ LOCAL_ Create an item named Thinger under machine \ software \ Microsoft \ Windows NT \ CurrentVersion.

RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.CreateSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Thinger");

3. Use C# delete registry key: delete the SOFTWARE\Microsoft\Windows NT\CurrentVersion\Thinger key in the registry.

RegistryKey key = Registry.LocalMachine;
key.DeleteSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Thinger",true);

C# operation registry key

Whether creating, obtaining or deleting a key value, you first need to open the registry key corresponding to the key value to be created or set

RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion",true); 

1. Create registry key value: add a key value in the above registry key. The key name is "thinker" and the value is "www.dotnetswj.com".

software.SetValue("Thinger","www.dotnetswj.com");

The SetValue() method has three parameters, setValue (key name, key value and key value type). The key value types include string, binary, Dword, etc. when the key value type is not set, it defaults to string type. If the key already exists in the registry, it will be overwritten after the operation.

2. Get registry key value: get the value corresponding to Thinger.

string GetValue = software.GetValue("Thinger").ToString();

3. Delete registry key value: delete the key value corresponding to Thinger.

software.DeleteValue("Thinger");

Before registering to delete a key value, ensure that the name of the deleted key value must exist, otherwise an exception will be thrown.

Finally, note that after each use, close the RegistryKey object used.

     key.Close();
     software.Close();

application

Finally, make an application and write a program to automatically delete the Siemens registry, so you don't need to delete it every time.

First open the registry key, and then delete the registry key.

     RegistryKey key = Registry.LocalMachine;

     //Open registry key
     RegistryKey software = key.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager", true);

     //Delete key value
     software.DeleteValue("PendingFileRenameOperations", true);

     key.Close();
     software.Close();

Posted by Gayner on Wed, 13 Oct 2021 20:39:43 -0700