- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.Win32;
- namespace WWBClassLib.PC
- {
- public class RegEdit
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="RegUrl">节点路径(全路径 HKEY_LOCAL_MACHINE/SOFTWARE ) 用 "/" 分隔</param>
- /// <param name="name">项目名称</param>
- /// <returns></returns>
- public static string GetRegistData(string RegUrl,string name)
- {
- string registData;
- char[] sp = {'/'};
- string[] nodes = RegUrl.Split(sp);
- RegistryKey root = null;
- switch (nodes[0].ToUpper())
- {
- case "HKEY_CLASSES_ROOT":
- root = Registry.ClassesRoot;
- break;
- case "HKEY_CURRENT_USER":
- root = Registry.CurrentUser;
- break;
- case "HKEY_LOCAL_MACHINE":
- root = Registry.LocalMachine;
- break;
- case "HKEY_USERS":
- root = Registry.Users;
- break;
- case "HKEY_CURRENT_CONFIG":
- root = Registry.CurrentConfig;
- break;
- }
- if (root == null)
- {
- return "error root";
- }
- RegistryKey node = root;
- for (int i = 1; i < nodes.Length; i++)
- {
- node = node.OpenSubKey(nodes[i], true);
- if (node == null)
- {
- return nodes[i] + "节点不存在";
- }
- }
- registData = node.GetValue(name).ToString();
- return registData;
- }
- /// <summary>
- /// 写入注册表值
- /// </summary>
- /// <param name="RegUrl">路径(全路径,不存在的节点自动创建)</param>
- /// <param name="name">名称</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- public static string WTRegedit(string RegUrl, string name, object value)
- {
- char[] sp = { '/' };
- string[] nodes = RegUrl.Split(sp);
- RegistryKey root = null;
- switch (nodes[0].ToUpper())
- {
- case "HKEY_CLASSES_ROOT":
- root = Registry.ClassesRoot;
- break;
- case "HKEY_CURRENT_USER":
- root = Registry.CurrentUser;
- break;
- case "HKEY_LOCAL_MACHINE":
- root = Registry.LocalMachine;
- break;
- case "HKEY_USERS":
- root = Registry.Users;
- break;
- case "HKEY_CURRENT_CONFIG":
- root = Registry.CurrentConfig;
- break;
- }
- if (root == null)
- {
- return "error root";
- }
- RegistryKey node = root;
- for (int i = 1; i < nodes.Length; i++)
- {
- if(!IsRegeditExit(node,nodes[i]))
- {
- node = node.CreateSubKey(nodes[i]);
- continue;
- }
- node = node.OpenSubKey(nodes[i], true);
- }
- node.SetValue(name, value);
- return "";
- }
- private static bool IsRegeditExit(RegistryKey node ,string name)
- {
- string [] subkeyNames = node.GetSubKeyNames();
- foreach (string keyName in subkeyNames)
- {
- if (keyName == name)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 删除注册表节点
- /// </summary>
- /// <param name="RegUrl">路径(全路径)</param>
- /// <param name="name">键值(节点值)</param>
- /// <returns></returns>
- public static string DeleteRegist(string RegUrl, string name)
- {
- char[] sp = { '/' };
- string[] nodes = RegUrl.Split(sp);
- RegistryKey root = null;
- switch (nodes[0].ToUpper())
- {
- case "HKEY_CLASSES_ROOT":
- root = Registry.ClassesRoot;
- break;
- case "HKEY_CURRENT_USER":
- root = Registry.CurrentUser;
- break;
- case "HKEY_LOCAL_MACHINE":
- root = Registry.LocalMachine;
- break;
- case "HKEY_USERS":
- root = Registry.Users;
- break;
- case "HKEY_CURRENT_CONFIG":
- root = Registry.CurrentConfig;
- break;
- }
- if (root == null)
- {
- return "error root";
- }
- RegistryKey node = root;
- for (int i = 1; i < nodes.Length; i++)
- {
- node = node.OpenSubKey(nodes[i], true);
- if (node == null)
- {
- return nodes[i] + "节点不存在";
- }
- }
- if (!IsRegeditExit(node, name))
- {
- return name + "键值不存在";
- }
- else
- {
- node.DeleteSubKeyTree(name);
- }
- return "";
- }
- }
- }
自己封装的注册表操作类
最新推荐文章于 2021-12-10 11:53:56 发布
本文介绍了一个 C# 类库中的注册表操作方法,包括读取、写入和删除注册表项的功能实现。
1363

被折叠的 条评论
为什么被折叠?



