using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
namespace Qsg
{
///
/// 注册表操作类
///
public class WRegisterTool
{
///
/// 获得根节点注册表
///
/// 根节点类型
///
public static RegistryKey GetRootRegisterKey(WRegisterRootKeyType rootKeyType)
{
switch (rootKeyType)
{
case WRegisterRootKeyType.HKEY_CLASSES_ROOT:
return Registry.ClassesRoot;
case WRegisterRootKeyType.HKEY_CURRENT_CONFIG:
return Registry.CurrentConfig;
case WRegisterRootKeyType.HKEY_CURRENT_USER:
return Registry.CurrentUser;
case WRegisterRootKeyType.HKEY_LOCAL_MACHINE:
return Registry.LocalMachine;
default:
throw new Exception("注册表类型未定义!");
}
}
///
/// 在指定注册表项下创建注册表子项
///
/// 父注册表项
/// 注册表路径
///
public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath)
{
RegistryKey returnKey = fatherKey.CreateSubKey(keyPath);
return returnKey;
}
///
/// 在指定注册表项下创建注册表子项
///
/// 父注册表项
/// 注册表路径
/// 要添加的注册表项值名称
/// 要添加的注册表项值
///
public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath, string keyValueName, string keyValue)
{
RegistryKey returnKey = CreateRegistryKey(fatherKey, keyPath);
returnKey.SetValue(keyValueName, keyValue);
return returnKey;
}
///
/// 在指定注册表项下创建注册表子项
///
/// 注册表根节点项类型
/// 注册表路径
///
public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath)
{
RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
return CreateRegistryKey(rootKeyType, keyPath);
}
///
/// 在指定注册表项下创建注册表子项
///
/// 注册表根节点项类型
/// 注册表路径
/// 要添加的注册表项值名称
/// 要添加的注册表项值
///
public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue)
{
RegistryKey returnKey = CreateRegistryKey(rootKeyType, keyPath);
returnKey.SetValue(keyValueName, keyValue);
return returnKey;
}
///
/// 删除注册表子项
///
/// 注册表根节点项类型
/// 注册表路径
///
public static bool DeleteRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyName)
{
RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
RegistryKey deleteKey = rootKey.OpenSubKey(keyPath);
if (IsKeyHaveSubKey(deleteKey, keyName))
{
deleteKey.DeleteSubKey(keyName);
return true;
}
return false;
}
///
/// 获取注册表项
///
/// 注册表根节点项类型
/// 注册表路径
///
public static RegistryKey GetRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath)
{
RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
return rootKey.OpenSubKey(keyPath);
}
///
/// 获取注册表项指定值
///
/// 注册表根节点项类型
/// 注册表路径
/// 要获得值的注册表值名称
///
public static string GetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyName)
{
RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
if (IsKeyHaveValue(key, keyName))
{
return key.GetValue(keyName).ToString();
}
return null;
}
///
/// 设置注册表项值
///
/// 注册表项
/// 值名称
/// 值
///
public static bool SetRegistryValue(RegistryKey key, string keyValueName, string keyValue)
{
key.SetValue(keyValueName, keyValue);
return true;
}
///
/// 设置注册表项值
///
/// 注册表根节点项类型
/// 注册表路径
/// 值名称
/// 值
///
public static bool SetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue)
{
RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
key.SetValue(keyValueName, keyValue);
return true;
}
///
/// 删除注册表项值
///
/// 注册表项
/// 值名称
/// 值
///
public static bool DeleteRegistryValue(RegistryKey key, string keyValueName)
{
if (IsKeyHaveValue(key, keyValueName))
{
key.DeleteValue(keyValueName);
return true;
}
return false;
}
///
/// 删除注册表项值
///
/// 注册表根节点项类型
/// 注册表路径
/// 值名称
///
public static bool DeleteRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName)
{
RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
if (IsKeyHaveValue(key, keyValueName))
{
key.DeleteValue(keyValueName);
return true;
}
return false;
}
///
/// 判断注册表项是否有指定的注册表值
///
/// 注册表项
/// 注册表值
///
private static bool IsKeyHaveValue(RegistryKey key, string valueName)
{
string[] keyNames = key.GetValueNames();
foreach (string keyName in keyNames)
{
if (keyName.Trim() == valueName.Trim())
{
return true;
}
}
return false;
}
///
/// 判断注册表项是否有指定的子项
///
/// 注册表项
/// 子项名称
///
private static bool IsKeyHaveSubKey(RegistryKey key, string keyName)
{
string[] subKeyNames = key.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
if (keyName.Trim() == keyName.Trim())
{
return true;
}
}
return false;
}
///
/// 根据注册表键路径获得注册表键名称
///
/// 注册表键路径
///
private static string GetKeyNameByPath(string keyPath)
{
int keyIndex = keyPath.LastIndexOf("/");
return keyPath.Substring(keyIndex + 1);
}
public enum WRegisterRootKeyType
{
HKEY_CLASSES_ROOT = 0,
HKEY_CURRENT_USER = 1,
HKEY_LOCAL_MACHINE = 2,
HKEY_USERS = 3,
HKEY_CURRENT_CONFIG = 4
}
}
}