daxueit.com logo

C#注册表操作范例(控制台)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Collections;
using System.Security.Permissions;

namespace regedit
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("按任意键开始开始创建注册表键值...");
            Console.ReadKey(true);
            // 在HKEY_CURRENT_USER下创建一个子键Test
            RegistryKey test = Registry.CurrentUser.CreateSubKey("Test");
            // 在Test下创建两个子键
            using (RegistryKey testName = test.CreateSubKey("Name"),testSettings = test.CreateSubKey("Settings"))
            {
                // 为子键赋值
                testSettings.SetValue("Sex", "Male");
                testSettings.SetValue("Age", "35");
                testSettings.SetValue("ID", 123);
            }
            Console.WriteLine("注册表键值创建成功!");

            // 输出创建的注册表信息
            Console.WriteLine("在{1}中有{0}个子键:",test.SubKeyCount.ToString(), test.Name);
            foreach (string subKeyName in test.GetSubKeyNames())
            {
                using (RegistryKey tempKey = test.OpenSubKey(subKeyName))
                {
                    Console.WriteLine("\n在子键中{1}有{0}个键值:",tempKey.ValueCount.ToString(), tempKey.Name);
                    foreach (string valueName in tempKey.GetValueNames())
                    {
                        Console.WriteLine("{0,-8}: {1}", valueName,tempKey.GetValue(valueName).ToString());
                    }
                }
            }

            using (RegistryKey testSettings = test.OpenSubKey("Settings", true))
            {
                Console.WriteLine("修改注册表键值...");
                // 修改键值
                testSettings.SetValue("Sex", "Female");
                testSettings.SetValue("Age", "30");
                testSettings.SetValue("ID", 456);
                // 输出修改后的信息
                Console.WriteLine("完成修改后的注册表键值:");
                foreach (string valueName in testSettings.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName,
                        testSettings.GetValue(valueName).ToString());
                }
            }

            using (RegistryKey testSettings = test.OpenSubKey("Settings", true))
            {
                Console.WriteLine("删除注册表子键ID...");
                // 删除ID键值
                testSettings.DeleteValue("id");
                // 输出确认信息
                Console.WriteLine((string)testSettings.GetValue("id", "ID not found."));
            }

            // 删除或关闭创建的子键
            Console.Write("\n即将删除所创建的注册表子键? (Y/N) ");
            if (Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
            {
                Registry.CurrentUser.DeleteSubKeyTree("Test");
                Console.WriteLine("\n注册表子键 {0} 已经被删除.", test.Name);
            }
            else
            {
                Console.WriteLine("\n注册表子键 {0} 已经被关闭.", test.ToString());
                test.Close();
            }

        }
    }
}