zoukankan      html  css  js  c++  java
  • C#使用Windows注册表编辑

    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.
  • 相关阅读:
    当数据库结构改变时,需要将数据库删除再创建
    命名空间“System.Web.Mvc”中不存在类型或命名空间“Ajax”(是否缺少程序集引用?)
    jqGrid 各种参数 详解
    二维数组最小路径和
    动态规划:最大连续子序列和
    最长递增子序列
    java单例模式的几种实现
    java多线程的实现方法
    sleep与wait的区别
    数组旋转
  • 原文地址:https://www.cnblogs.com/Tonyyang/p/1370520.html
Copyright © 2011-2022 走看看