C # use Windows Registry to edit

Source: Internet
Author: User
Using Microsoft. Win32;

Creating subkeys in the Registry

We create a subkey by using the createsubkey method. We usually want to create a subkey in HKEY_CURRENT_USER/ Software That is why we use the following code:

 


RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Geekpedia\\Test");

Adding Values to subkeys

Now we can add a value to the subkey by using the following piece of code:

 


reg.SetValue("URL", "http://www.geekpedia.com");

As you can see, the code is rather straightforward. We create a value with the name 'url' and the data 'HTTP: // www.geekpedia.com '.

Retrieving data

Further, we shall retrieve the value we just added and store it in a string variable:

 


string URL = (string)reg.GetValue("URL");

... And display the output:


Console.WriteLine("Before: " + URL);

Changing the values

We change the value of 'reg '. we specify the same path, but now we also add a 'true' Boolean at the end. that means we use the overloaded method and 'true' means we want the path in write mode.
The overloaded method prototype is:

 


public RegistryKey OpenSubKey(string name, bool writable);

And here is the code we use:


RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Geekpedia\\Test", true);

Because we opened the subkey in write mode, we can modify the data:


reg.SetValue("URL", "about:blank");

And store it again in the same variable:


URL = (string)reg.GetValue("URL");

... And display the output:


Console.WriteLine("After: " + URL);

And finally close the opened connection with the registry subkey and flush:


reg.Close();

Finally, all the Code put together looks like this:


using System;
using Microsoft.Win32;
class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create a new SubKey or open it if already exists
        RegistryKey reg = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Geekpedia\\Test");
        // Add a new value to the SubKey
        reg.SetValue("URL", "http://www.geekpedia.com");
        // Store it in 'URL' variable
        string URL = (string)reg.GetValue("URL");
        // Display it
        Console.WriteLine("Before: " + URL);

        // Open the SubKey this time in write mode
        reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Geekpedia\\Test", true);
        // Change the value 'URL'
        reg.SetValue("URL", "about:blank");
        // Store it in the earlier variable
        URL = (string)reg.GetValue("URL");
        // Display it
        Console.WriteLine("After: " + URL);
        // Close the registry SubKey and flush
        reg.Close();
    }
}

Deleting subkeys

For removing subkeys from the Registry you can use the method with the following prototype:

 


public void DeleteSubKey(string subkey);

And here is a hands-on example:


// Open SOFTWARE
RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE");
// Delete Geekpedia from SOFTWARE
reg.DeleteSubKeyTree("Geekpedia");
//Close the registry SubKey and flush
reg.Close();

If the key doesn't exist you will get a nasty exception. for ignoring the exception use the overloaded method:


public void DeleteSubKey(string subkey, bool throwOnMissingSubKey);

In our example this looks like this:


reg = Registry.CurrentUser.OpenSubKey("SOFTWARE", false);

Deleting values

Deleting a value is similar to deleting a subkey, it also has two prototypes:

 


public void DeleteValue(string name);
public void DeleteValue(string name, bool throwOnMissingValue);

Which act the same as in deletesubkey.

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.