//************************************************************************
// 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
{
///
/// 注册表操作工具
///
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;
}
}
///
/// 添加注册表值
///
///
///
///
///
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;
}
}
///
/// 读取注册表值
///
///
///
///
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;
}
///
/// 删除注册表值
///
///
///
///
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;
}
}
///
/// 分离注册表根目录和子目录
///
///
///
///
///
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;
}
///
/// 打开注册表相应目录
///
/// 目标子项
/// 是否具有写权限
///
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);
}
///
/// 创建或打开注册表相应目录
///
/// 目标子项
///
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);
}
///
/// 判断是否存在项、键、值匹配
///
///
///
///
///
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;
}
}
}