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();
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值