C#. How to operate registry RegistryKey in net

Source: Internet
Author: User
Tags delete key reflection win32

Look at the help of RegistryKey, this thing is not complicated, such as:

1, add key plus value

01.string appName = "Poweroffontime";
02.//gets the assembly that executes the method and gets the file path of the assembly (the directory where the assembly is located by the file path)   
03.string Thisexecutablepath = System.Reflection.Assembly.GetExecutingAssembly (). Location;  
This path in the 04.//software\\microsoft\\windows\\currentversion\\run registry is the boot-initiated path   
05.microsoft.win32.registrykey Rkey = +  
.    Microsoft.Win32.Registry.LocalMachine.CreateSubKey  
.    ("Software\\microsoft\\windows\\currentversion\\run");  
08.rkey.setvalue (AppName, thisexecutablepath);   
09.rkey.close ();  

The effect is as follows:

2, read the key value

Microsoft.Win32.RegistryKey Rkey =  
                   Microsoft.Win32.Registry.LocalMachine.CreateSubKey  
                   ("software\\ Microsoft\\windows\\currentversion\\run ");
String  Key = Rkey.getvalue ("Poweroffontime"). ToString ();

This paper url:http://www.bianceng.cn/programming/csharp/201410/45593.htm

The effect is as follows:

3. Create a new file in the registry and add a key value under the folder

String appName = "Poweroffontime";  
Gets the assembly that executes the method, and gets the file path of the assembly (the directory where the assembly is located by the file path)  
string thisexecutablepath = System.Reflection.Assembly.GetExecutingAssembly (). Location;  
RegistryKey src = Registry.LocalMachine.OpenSubKey ("SOFTWARE", true). OpenSubKey ("Microsoft", true). OpenSubKey ("Windows", true). OpenSubKey ("CurrentVersion", true). OpenSubKey ("Run", true);  
Write to the registry key (that is, the folder)  
RegistryKey red = src. CreateSubKey ("Poweroffontime");  
Write the value red in this folder  
. SetValue (AppName, Thisexecutablepath);

The effect is as follows:

4, delete key

RegistryKey src = Registry.LocalMachine.OpenSubKey ("SOFTWARE", true). OpenSubKey ("Microsoft", true). OpenSubKey ("Windows", true). OpenSubKey ("CurrentVersion", true). OpenSubKey ("Run", true);

Src. DeleteSubKey ("Poweroffontime");

The effect is as follows:

Note

Modify the registry, especially the boot automatically startup registry The biggest advantage is that when you write small programs, you can automatically perform every day, such as: you want to write a time when the automatic shutdown of the small program ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.