| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- //************************************************************************
- // author: yuzhengyang
- // date: 2017.3.29 - 2017.6.13
- // desc: 注册表操作工具
- // Copyright (c) yuzhengyang. All rights reserved.
- //************************************************************************
- using Azylee.Core.DataUtils.StringUtils;
- using Microsoft.Win32;
- using System;
- namespace Azylee.Core.WindowsUtils.RegisterUtils
- {
- /// <summary>
- /// 注册表操作工具
- /// </summary>
- public class RegisterTool
- {
- [Obsolete("SetValue",true)]
- public static bool Write(string key, string name, string value)
- {
- try
- {
- RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
- if (RKey == null)
- RKey = Registry.LocalMachine.CreateSubKey(key);
- RKey.SetValue(name, value);
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- [Obsolete("GetValue", true)]
- public static string Read(string key, string name)
- {
- try
- {
- RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
- if (RKey != null)
- {
- return RKey.GetValue(name) != null ? RKey.GetValue(name).ToString() : "";
- }
- }
- catch (Exception e) { }
- return null;
- }
- [Obsolete("DeleteValue", true)]
- public static bool Delete(string key, string name)
- {
- try
- {
- RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
- if (RKey != null)
- RKey.DeleteValue(name);
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- /// <summary>
- /// 添加注册表值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool SetValue(string key, string name, string value)
- {
- try
- {
- using (RegistryKey RKey = Create(key))
- {
- RKey.SetValue(name, value);
- }
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- /// <summary>
- /// 读取注册表值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public static string GetValue(string key, string name)
- {
- try
- {
- using (RegistryKey RKey = Open(key, false))
- {
- if (RKey != null)
- {
- return RKey.GetValue(name) != null ? RKey.GetValue(name).ToString() : null;
- }
- }
- }
- catch (Exception e) { }
- return null;
- }
- /// <summary>
- /// 删除注册表值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool DeleteValue(string key, string name)
- {
- try
- {
- using (RegistryKey RKey = Open(key, true))
- {
- if (RKey != null)
- RKey.DeleteValue(name);
- }
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- /// <summary>
- /// 分离注册表根目录和子目录
- /// </summary>
- /// <param name="key"></param>
- /// <param name="reg"></param>
- /// <param name="sub"></param>
- /// <returns></returns>
- private static bool ExtractInfo(string key, out string reg, out string sub)
- {
- reg = ""; sub = "";
- int splitPos = 1;
- if ((splitPos = key.IndexOf('\\')) > 0)
- {
- reg = key.Substring(0, splitPos);
- sub = key.Substring(splitPos + 1);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 打开注册表相应目录
- /// </summary>
- /// <param name="key">目标子项</param>
- /// <param name="writable">是否具有写权限</param>
- /// <returns></returns>
- private static RegistryKey Open(string key, bool writable)
- {
- string regkey, subkey;
- if (ExtractInfo(key, out regkey, out subkey))
- {
- switch (regkey)
- {
- case "HKEY_CLASSES_ROOT": return Registry.ClassesRoot.OpenSubKey(subkey, writable);
- case "HKEY_CURRENT_USER": return Registry.CurrentUser.OpenSubKey(subkey, writable);
- case "HKEY_LOCAL_MACHINE": return Registry.LocalMachine.OpenSubKey(subkey, writable);
- case "HKEY_USERS": return Registry.Users.OpenSubKey(subkey, writable);
- case "HKEY_CURRENT_CONFIG": return Registry.CurrentConfig.OpenSubKey(subkey, writable);
- default: return Registry.CurrentUser.OpenSubKey(subkey, writable);
- }
- }
- return Registry.CurrentUser.OpenSubKey(subkey, writable);
- }
- /// <summary>
- /// 创建或打开注册表相应目录
- /// </summary>
- /// <param name="key">目标子项</param>
- /// <returns></returns>
- private static RegistryKey Create(string key)
- {
- string regkey, subkey;
- if (ExtractInfo(key, out regkey, out subkey))
- {
- switch (regkey)
- {
- case "HKEY_CLASSES_ROOT": return Registry.ClassesRoot.CreateSubKey(subkey);
- case "HKEY_CURRENT_USER": return Registry.CurrentUser.CreateSubKey(subkey);
- case "HKEY_LOCAL_MACHINE": return Registry.LocalMachine.CreateSubKey(subkey);
- case "HKEY_USERS": return Registry.Users.CreateSubKey(subkey);
- case "HKEY_CURRENT_CONFIG": return Registry.CurrentConfig.CreateSubKey(subkey);
- default: return Registry.CurrentUser.CreateSubKey(subkey);
- }
- }
- return Registry.CurrentUser.CreateSubKey(subkey);
- }
- /// <summary>
- /// 判断是否存在项、键、值匹配
- /// </summary>
- /// <param name="key"></param>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool Exist(string key, string name, string value)
- {
- if (Str.Ok(key) && Str.Ok(name))
- {
- string val = RegisterTool.GetValue(key, name);
- if (Str.Ok(value))
- {
- if (Str.Ok(val) && value == val) return true;
- }
- else
- {
- if (val != null) return true;
- }
- }
- return false;
- }
- }
- }
|